Receiving push notification in iOS when the app is closed using GCM - ios

My app receives push notifications using GCM. My android app works perfectly in that when i completely close the app i still receive notifications however if i do the same with my iOS version i don't receive anything.
Note this will of course be after the app has registered for the first time and then the app is closed. I cant find much information about this. some posts are saying you cant do it. but the likes of facebook and facebook messenger all do it. Does anyone know how this is done?
this is my
DidReceiveRemoteNotification method which is taken from the GCM documentation
// [START ack_message_reception]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// [START_EXCLUDE]
[[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
object:nil
userInfo:userInfo];
// [END_EXCLUDE]
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
{
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// [START_EXCLUDE]
[[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
object:nil
userInfo:userInfo];
handler(UIBackgroundFetchResultNoData);
// [END_EXCLUDE]
}
// [END ack_message_reception]
my payload sent looks like this
$payload = array('registration_ids' => $Regids,
'content_available' => false,
'notification' => array("body"=> $_POST['message'],
"sound" => "default",
'badge' => '1'
)
);
note this works if the app is in the background

I managed to get this working by adding this to my pay load
"priority" => "high"
maybe someone can comment why this worked as i have no idea

It also does work with iOS when the app is closed - if it doesn't, something is wrong.
However, you cannot use silent pushes with iOS when the app was closed by the user (up-swipe in task switcher) - so if your app wants to react on a push without the user having to tap on it, you can't do that when the app was closed.

I Think you are not specifying the alert key in the notification payload. The message provided in the alert key of the notification payload will be displayed on the device when the app is closed.
{
"aps" : { "alert" : "Message received from Bob" },
"acme2" : [ "bang", "whiz" ]
}
In the above payload check the alert key. The Message Message received from Bob will be displayed in the notification center of the iPhone when the app is closed.

Related

Cannot receive notification in iOS using Firebase

I'm using the Firebase console in testing to receive a notification in my iOS. At first it says that I'm connected to Firebase and I receive the Message ID but then I'm having an error Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called. and Unable to connect to FCM. Error Domain=com.google.fcm Code=2001 "(null)" How can I solve this?
Here is my code for didReceiveRemoteNotification
// [START receive_message]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
NSLog(#"Message ID: %#", userInfo[#"gcm.message_id"]);
// Pring full message.
NSLog(#"%#", userInfo);
}
// [END receive_message]
Try adding:
completionHandler(UIBackgroundFetchResultNoData);
At the end of your didReceiveRemoteNotification method.

Where should I handle Push Notificaiotn while using GCM ios 9

I have set up my app to work with GCM.
I have successfully added the code to integrate the GCM in my App.
Now I have two methods to handle the Push Notification:
Default Method
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
}
GCM Method
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// ...
}
Now I am confused where should I Handle my Notificaiton.
Where should I check application state and call my method to handle it.
Should I have to write method in both of these methods.
I'm not familiar with GCM but the two notification methods you listed standard UIApplicationDelegate methods and handle different scenarios.
application:didReceiveRemoteNotification: is called when the app is open and you receive a plain push notification. The types that you get alerted through notification center.
application:didReceiveRemoteNotification:fetchCompletionHandler: is called when the server is letting the app know there's something to download. You check the userInfo for what to download, initiate the download and call the handler(UIBackgroundFetchResult) upon NewData/NoData/Failed
Not sure what GCM does with these two methods but with that info you should be able to figure it out.
You should use GCM Method.
(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
}
In this method, You can handle your notification.
For example,
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// [START_EXCLUDE]
if(application.applicationState == UIApplicationStateBackground){
//app is in background
}else if(application.applicationState == UIApplicationStateInactive){
//From background to foreground (user touchs notification)
}
handler(UIBackgroundFetchResultNoData);
// [END_EXCLUDE]
}

Print NSlog output when app is closed

I am having an iOS application where I am receiving Push Notifications. I have the following code in AppDelegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if ( application.applicationState == UIApplicationStateActive )
{
// app was already in the foreground
}
else
{
NSLog(#"Received push notification");
}
}
I am running the app in Xcode with my iPhone. When the app is in background I am able see the NSLog when the notification comes.
When I close the app i.e removing it from opened apps in iOS the debugger session in Xcode stops and I can't see the NSlog after that even after receiving notification on my iPhone for the app when the app is closed (Push Notifications can come to app even if it is closed)
How can I print logs when the app is being opened from closed state?
It sounds like you're describing a few different things here.
First, you won't be able to see any NSLog notification messages if/after the app is fully terminated, until the app is launched by some mechanism.
If you want to print logs when the app is being opened from a terminated state, you can do something like the following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
NSLog(#"%s", __PRETTY_FUNCTION__); // print the function name
// Process remote notifications
NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) {
[self processNotification:remoteNotification];
}
}
- (void)processNotification:(NSDictionary *)payload {
NSString *someString = [payload objectForKey:#"someStringKey"];
// do something!
}

Silent remote notifications with bluemix for ios

Is there a way to send silent or mixed remote notifications with bluemix? There is no such option in the dashboard.
I want my app to fetch data while it's in background when receiving a remote notification.
Edit (copy-paste from comment):
I meant what is the way to send mixed push notifications from Bluemix side, not how to handle it client side. The solution is to use REST API:
POST https://mobile.eu-gb.bluemix.net/imfpush/v1/apps/$(app_id)/messages
with body:
"settings": { "apns": { "type":'MIXED' }
Handling of notifications is done client-side and what you're wanting to do is certainly possible. Taking from the Bluemix Documentation for Push Notifications linked here
Silent notifications do not appear on the device screen. These notifications are received by the application in the background, which wakes up the application for up to 30 seconds to perform the specified background task. A user might not be aware of the notification arrival.
To handle silent push notifications, implement the following method in appDelegate.m.
// For Objective C
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
NSNumber *contentAvailable = userInfo[#"aps"][#"content-available"];
if([contentAvailable intValue]== 1){
[[IMFPushClient sharedInstance] application:application didReceiveRemoteNotification:userInfo];
//Perform background task
NSLog(#"Received a silent push..");
NSLog(#"userInfo: %#", userInfo.description);
_appDelegateVC.result.text = userInfo.description;
handler(UIBackgroundFetchResultNewData);
}
else{
//Normal Notification
[[IMFPushAppManager get] notificationReceived:userInfo];
NSLog(#"Received a normal notification.");
NSLog(#"userInfo: %#", userInfo.description);
_appDelegateVC.result.text = userInfo.description;
handler(UIBackgroundFetchResultNoData);
}
//Success
}
The contentAvailable value that is sent by the server for silent notifications is equal to 1.

App doesn't receive remote notification when it is not running

My app doesn't receive push notifications when it's not running.
I am trying to handle remote notification sent as JSON and update data in my app using data from given JSON.
All is going well when app is active or in background.
But when app is not running, app is processing notifications only when I open my app by tapping on notification, but not when I open app by tapping on icon.
Here is the code from appDelegate class:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Parse setApplicationId:appId
clientKey:clKey];
if (application.applicationState != UIApplicationStateBackground) {
BOOL preBackgroundPush = ![application respondsToSelector:#selector(backgroundRefreshStatus)];
BOOL oldPushHandlerOnly = ![self respondsToSelector:#selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)];
BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
}
}
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
UIRemoteNotificationTypeAlert|
UIRemoteNotificationTypeSound|
UIRemoteNotificationTypeNewsstandContentAvailability];
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
[self processPushNotification:notificationPayload foreground:YES];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
TFLog(#"didRegisterForRemoteNotificationsWithDeviceToken");
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
TFLog(#"deviceToken: %#, currentInstallation.badge: %ld", currentInstallation.deviceToken, (long)currentInstallation.badge);
TFLog(#"deviceToken: %#, deviceType: %#", currentInstallation.deviceToken, currentInstallation.deviceType);
TFLog(#"installationId: %#", currentInstallation.installationId);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
TFLog(#"didFailToRegisterForRemoteNotificationsWithError %#", error);
if (error.code == 3010) {
TFLog(#"Push notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register.
TFLog(#"application:didFailToRegisterForRemoteNotificationsWithError: %#", error);
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
TFLog(#"%#", userInfo);
[PFPush handlePush:userInfo];
[self processPushNotification:userInfo foreground:YES];
[PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
TFLog(#"didReceiveRemoteNotification2");
[self processPushNotification:userInfo foreground:YES];
[PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}
As result, app is receiving remote notification in all states, excepting when it is not running.
What have I missed?
You have missed the bit in the Local and Push Notification Programming Guide where it says -
If the action button is tapped (on a device running iOS), the system
launches the application and the application calls its delegate’s
application:didFinishLaunchingWithOptions: method (if implemented); it
passes in the notification payload (for remote notifications) or the
local-notification object (for local notifications).
If the application icon is tapped on a device running iOS, the
application calls the same method, but furnishes no information about
the notification
Also, this note from Apple -
Important: Delivery of notifications is a “best effort”, not
guaranteed. It is not intended to deliver data to your app, only to
notify the user that there is new data available.
If your application is launched from the app icon rather than the notification you need to check for updated content independent of any push notification that may have been received. This enables an application to behave differently when it opens from a notification and when it is opened from the app icon.
For example, the Facebook app opens directly to the item in the notification when launched from the notification alert but not when launched from the app icon - which is the "correct" behaviour from a user point of view. If I interact with the notification then I am interested in its content. If I launch the app from the icon then I just want to use the app - I can then access the notifications in the app if I want.
There's no way you can get the information about your push notification JSON payload when you launch the app by explicitly tapping the app icon.
That's as per Apple's design. When you open the application from any push notification action, then you can retrieve the push notification in application: didFinishLaunchingWithOptions: delegate method. Typically you look up for the UIApplicationLaunchOptionsRemoteNotificationKey key in your launchOptions dictionary.
But, when you open the app by explicitly tapping the application icon, though still the application: didFinishLaunchingWithOptions: delegate will be called, the UIApplicationLaunchOptionsRemoteNotificationKey key will return nil.

Resources