【问题标题】:Ionic Push Notification event handlers. Which to use?离子推送通知事件处理程序。使用哪个?
【发布时间】:2017-06-04 05:35:24
【问题描述】:

我正在为 Android 和 iOS 使用 Ionic 推送通知。

在 Android 上,无论是手动触发还是通过 API 以编程方式触发,它们都可以正常工作。在 iOS 上,它们似乎只能通过 Ionic Cloud 手动进入。到目前为止,我不确定是什么原因造成的,但是我在文档中注意到了一些我不确定的东西。

在推送通知的 Ionic Docs 中,当通知进入应用程序时,似乎有两个不同的事件处理程序。一个是我目前正在使用的:

$scope.$on('cloud:push:notification', function(event, data) {
  var msg = data.message;
  alert(msg.title + ': ' + msg.text);
});

和另一个人

push.on('notification', function(data) {
    // do something with the push data
    // then call finish to let the OS know we are done
    push.finish(function() {
        console.log("processing of push data is finished");
    }, function() {
        console.log("something went wrong with push.finish for ID = " + data.additionalData.notId)
    }, data.additionalData.notId);
});

前者需要$ionicPush注入,而后者初始化如下:

var push = PushNotification.init({
       "android": {
           "senderID": "XXXXXXXXXXX",
           "sound": "true",
           "vibrate": "true"
       },
       "ios": {
           "alert": "true",
           "badge": "true",
           "sound": "true"
        },
        "windows": {}
})

谁能告诉我应该使用哪个事件处理程序?我认为这可能与我在 iOS 上没有收到程序化推送有关

注意:是的,我在 Xcode 上启用了推送通知和远程通知。请记住,我在 iOS 上收到手动推送,而不是程序化推送。

【问题讨论】:

  • ios 手动推送是什么意思?
  • 使用仪表板上的 Ionic Cloud 界面发送
  • 哦,程序化正在通过 APNS 发送?

标签: android ios ionic-framework push-notification apple-push-notifications


【解决方案1】:

事实证明,它的文档错误。以下解决方案似乎有效。

    $rootScope.$on('cloud:push:notification', function(event, data) {
    var msg = data.message;
    console.log("data = "+JSON.stringify(data));

    if (data.raw.additionalData["content-available"] == 1) {
      console.log("Found silent push notification, for platform "+ionic.Platform.platform());
     // handle the notification and then...
      $ionicPush.plugin.finish(function(result) {
             console.log("Finish successful with result " + result);
           }, function(error) {
             console.log("Finish unsuccessful with result "+error);
           });
    };
    });

PS:这是在离子论坛上,你可以找到原始帖子的链接here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多