How to Resolve GCM Notifications Not Received When App Is in Background Mode in iOS?

Linda Hamilton
Release: 2024-10-20 15:28:30
Original
348 people have browsed it

How to Resolve GCM Notifications Not Received When App Is in Background Mode in iOS?

GCM Notifications Not Received When App Is in Background Mode in iOS

Notifications may not be received when the app is in background mode due to the following two reasons:

  1. Incorrect Background Notification Configuration: To receive notifications while the app is in the background, you must configure Content-Available to true and set a non-zero Priority in your PHP script.
  2. GCM Service Disconnection: The GCM service must be connected when the app enters the background to receive push notifications. In your AppDelegate.m file, ensure that the applicationDidEnterBackground method includes code to reconnect to the GCM service.

Solution:

In your PHP file, add the following to the $post array:

<code class="php">'content_available' => true,
'priority' => 'high',</code>
Copy after login

Also, update the $data array to include a body field:

<code class="php">$data = array( 'message' => 'Hello World!', 'body' => 'Hello World!');</code>
Copy after login

In your AppDelegate.m file, ensure that the following code is present in the applicationDidEnterBackground method:

<code class="objective-c">[[GCMService sharedInstance] connectWithHandler:^(NSError *error) {
    if (error) {
        NSLog(@"Could not connect to GCM: %@", error.localizedDescription);
    } else {
        _connectedToGCM = true;
        NSLog(@"Connected to GCM");
    }
}];</code>
Copy after login

By implementing these changes, your app should now receive notifications even when it is in the background.

The above is the detailed content of How to Resolve GCM Notifications Not Received When App Is in Background Mode in iOS?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!