Home >Backend Development >PHP Tutorial >How to Resolve GCM Notifications Not Received When App Is in Background Mode in iOS?

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

Linda Hamilton
Linda HamiltonOriginal
2024-10-20 15:28:30450browse

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>

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

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

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>

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!

Statement:
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