I'm working on getting background notifications for working with IOS using GCM - background notifications no longer work. Here are my steps for integrating background notifications:
- Enable Remote Notification Tag in UIBackgroundmodes
- Add a content-accessible key to my payload.
- Burn application: didRecieveRemoteNotification: fetchCompletionHandler: in my delegate.
Here is the delegate function code:
func application( application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) { println("Notification received2: \(userInfo)") GCMService.sharedInstance().appDidReceiveMessage(userInfo); NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil, userInfo: userInfo) handler(UIBackgroundFetchResult.NoData); }
This is the code for server-side php script:
<?php $regId = $_GET["regId"]; $message = $_GET["message"]; $data = array( 'price' => $message, 'sound' => 'default', 'body' => 'helloworld', 'title' => 'default', 'badge' => 12, 'content-available' => 1); $ids = array( $regId); sendGoogleCloudMessage( $data, $ids ); function sendGoogleCloudMessage( $data, $ids ) { $apiKey = THE-API-KEY-THAT-I-AM-USING; $url = 'https://android.googleapis.com/gcm/send'; $post = array( 'registration_ids' => $ids, 'data' => $data, 'content-available' => 1, ); $headers = array( 'Authorization: key=' . $apiKey, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) ); $result = curl_exec( $ch ); if ( curl_errno( $ch ) ) { echo 'GCM error: ' . curl_error( $ch ); } curl_close( $ch ); echo $result; } ?>
I tried to send a flag for the availability of content through the internal array "data" and the external array "post", which I indicated by adding it to both.
The message was not received by the fetchCompletionHandler function, but waits until the application is activated again and received by the regular application: didRecieveRemoteNotification function. What could be the reason that my notification was not received through the background functions?