如何处理离子cordova应用中的静音推送通知?

时间:2015-09-09 07:44:56

标签: ionic-framework ionic cordova-plugins

我正在关注离子文档以创建离子push notifications。当应用程序处于活动状态时,此工作正常。当应用程序处于后台时应用程序收到推送通知时,我需要运行一个函数。

$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.
  }
});

我面临的问题是当应用程序处于后台时,onNotification回调函数不会触发。如何使用离子推送通知API实现这一目标?

2 个答案:

答案 0 :(得分:1)

在这种情况下,只有在您打开应用程序的托盘中单击通知时才会触发onNotification。如果通知在Android托盘中,则表示尚未看到通知,因此除非您点击它,否则它将永远不会到达应用程序。

答案 1 :(得分:0)

我怀疑这个补丁可以解决你的问题:

https://github.com/phonegap-build/PushPlugin/issues/288 (如果它消失,请引用此处)

    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;
    }
}

假设你愿意让它适应它 当前未弃用的插件:

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