Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework 如何在ionic cordova应用程序中处理静默推送通知?_Ionic Framework_Ionic_Cordova Plugins - Fatal编程技术网

Ionic framework 如何在ionic cordova应用程序中处理静默推送通知?

Ionic framework 如何在ionic cordova应用程序中处理静默推送通知?,ionic-framework,ionic,cordova-plugins,Ionic Framework,Ionic,Cordova Plugins,我正在遵循ionic文档创建ionic。当应用程序处于活动状态时,此功能正常工作。当应用程序在后台收到推送通知时,我需要运行一个函数 $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? canPlay

我正在遵循ionic文档创建ionic。当应用程序处于活动状态时,此功能正常工作。当应用程序在后台收到推送通知时,我需要运行一个函数

$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实现这一点

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

我怀疑此修补程序可能会解决您的问题:

(此处引用以防其消失)

假设您愿意调整它以与 当前未弃用的插件:


是的……但我想在收到设备的推送通知时执行一些功能。就像在静默推送通知中,它会在后台唤醒应用程序一样,我希望当推送到设备时,应用程序在后台时触发ionic事件。在这方面有什么帮助或建议吗?@Aruna我不知道这是否可能,这正是通知的目的,它允许用户在自己同意的情况下单击通知,仅在需要时激活应用程序。但是推送通知不能帮助您,这是我确信的。@Aruna如果能澄清您对推送通知的困惑,请接受答案。我建议你就你的实际需求提出新的问题。我很欣赏你的建议。我的问题是“如何在ionic cordova应用程序中处理静默推送通知?”我没有得到解决方案。有一种叫做静默推送通知的东西,当设备收到静默推送通知时,它会唤醒应用程序。所以我在等待答案。
    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;
    }
}