【问题标题】:How to handle silent push notification in ionic cordova application?如何处理离子科尔多瓦应用程序中的静默推送通知?
【发布时间】:2016-12-23 02:26:14
【问题描述】:

我正在关注 ionic 文档来创建 ionic 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 实现这一目标?

【问题讨论】:

    标签: ionic-framework ionic cordova-plugins


    【解决方案1】:

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

    【讨论】:

    • 是的...但是当设备收到推送通知时,我想做一些功能。就像在后台唤醒应用程序的静默推送通知一样,我希望当应用程序在后台时,当推送到达设备时触发 ionic 事件。对此有何帮助或建议?
    • @Aruna 我不知道这是否可能,这正是通知的目的,让用户仅在需要时通过自己同意单击通知来激活应用程序。但推送通知无法帮助您,我确信这一点。
    • @Aruna 如果它消除了您对推送通知的困惑,请接受答案。我会建议您发布有关您的实际要求的新问题。
    • 我很欣赏你的建议。我没有得到解决我的问题的解决方案,即“如何在 ionic cordova 应用程序中处理静默推送通知?”。有一种叫做静默推送通知的东西,当设备收到静默推送通知时,它会唤醒应用程序。所以我在等待答案。
    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 2017-09-06
      • 1970-01-01
      • 2015-11-24
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      相关资源
      最近更新 更多