Aruna
Aruna

Reputation: 2052

How to handle silent push notification in ionic cordova application?

I'm following ionic documentation to create ionic push notifications. This works fine when the app is in active state. I need to run a function when the app receive a push notification while the app is in background.

$ionicPush.register({
  canShowAlert: false, //Should new pushes show an alert on your screen?
  canSetBadge: true, //Should new pushes be allowed to update app icon badges?
  canPlaySound: false, //Should notifications be allowed to play a sound?
  canRunActionsOnWake: true, // Whether to run auto actions outside the app,
  onNotification: function(notification) {
    // Called for each notification.
  }
});

The issue i'm facing is onNotification callback function does not firing when the app is in background. How do I achieve that using ionic push notification API?

Upvotes: 1

Views: 2696

Answers (2)

Steve Madere
Steve Madere

Reputation: 509

I suspect this patch may solve your problem:

https://github.com/phonegap-build/PushPlugin/issues/288 (quoted here in case it disappears)

    We have managed to get this plugin to respond to silent push notifications which in turn call javascript functions, all while running in background mode. I thought we would share our solution as it seems like a lot of people are wanting this feature.

In AppDelegate+notification.m, change didReceiveRemoteNotification:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
    NSLog(@"didReceiveNotification");

    // Get application state for iOS4.x+ devices, otherwise assume active
    UIApplicationState appState = UIApplicationStateActive;
    if ([application respondsToSelector:@selector(applicationState)]) {
        appState = application.applicationState;
    }

    PushPlugin *pushHandler = [self getCommandInstance:@"PushPlugin"];
    pushHandler.notificationMessage = userInfo;
    if (appState == UIApplicationStateActive)
        pushHandler.isInline = YES;
    else
        pushHandler.isInline = NO;
    [pushHandler notificationReceived];

    handler(UIBackgroundFetchResultNewData);
}
In Pushplugin.m, change notificationReceived:

- (void)notificationReceived {
    NSLog(@"Notification received");

    if (notificationMessage && self.callback)
    {
        NSMutableString *jsonStr = [NSMutableString stringWithString:@"{"];

        [self parseDictionary:notificationMessage intoJSON:jsonStr];

        if (isInline)
        {
            [jsonStr appendFormat:@"foreground:\"%d\"", 1];
            isInline = NO;
        }
        else
            [jsonStr appendFormat:@"foreground:\"%d\"", 0];

        [jsonStr appendString:@"}"];

        NSLog(@"Msg: %@", jsonStr);

        NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", self.callback, jsonStr];
        [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];

        //get javascript function to fire in background mode
        CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonStr];
        [self.commandDelegate sendPluginResult:commandResult callbackId:self.callbackId];

        self.notificationMessage = nil;
    }
}

Assuming you are willing to adapt it to work with the current non-deprecated plugin:

https://github.com/phonegap/phonegap-plugin-push

Upvotes: 0

Mudasser Ajaz
Mudasser Ajaz

Reputation: 6257

In this case, onNotification will be only fired when you click on notification in tray, which open applications. If notification is in android tray, it means notification has not been seen, because of which it will never reach application unless you click on it.

Upvotes: 1

Related Questions