首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS使用cordova推送通知。从未收到通知。

iOS使用cordova推送通知。从未收到通知。
EN

Stack Overflow用户
提问于 2018-11-02 14:46:25
回答 1查看 2.6K关注 0票数 0

我创建了一个示例Cordova应用程序,它使用“phonegap-plugin”。这个应用程序没有任何复杂性。在"deviceready“上,我运行插件初始化代码,如下所示:

代码语言:javascript
复制
var push = PushNotification.init({android: {}, ios: {

    sound: true,
    alert: true,
    badge: true,
    categories: {
      invite: {
        yes: {
          callback: 'accept',
          title: 'Accept',
          foreground: true,
          destructive: false
        },
        no: {
          callback: 'reject',
          title: 'Reject',
          foreground: true,
          destructive: false
        },
        maybe: {
          callback: 'maybe',
          title: 'Maybe',
          foreground: true,
          destructive: false
        }
      },
      delete: {
        yes: {
          callback: 'doDelete',
          title: 'Delete',
          foreground: true,
          destructive: true
        },
        no: {
          callback: 'cancel',
          title: 'Cancel',
          foreground: true,
          destructive: false
        }
      }
    }
}})

push.on('notification', data => {
    console.log(data.message);
    console.log(data.title);
    console.log(data.count);
    console.log(data.sound);
    console.log(data.image);
    console.log(data.additionalData);
})

push.on('emailGuests', data => {
  console.log('I should email my guests');
});

push.on('snooze', data => {
  console.log('Remind me later');
});

push.on('registration',  data => {
  console.log(data.registrationId);
  console.log(data.registrationType);
});

push.subscribe('xx', console.log)

这是控制台的日志输出:

代码语言:javascript
复制
=> Successfully subscribe to topic xx
// The first run (after app install) will ask for permissions. If I click allow the lines below are printed to console.
=> dCAtjhCFBcU:APA91bG90c8VhNl_BzZ-2e9fmq_9fN6jfrRNJ1LPCRIpKnZ-AG-eLY4xtX84oJRZBh2D....KtNNQ35GM8ubPF5zr8HqeB6jffs
=> FCM

为了推送,我将向Legacy https://fcm.googleapis.com/fcm/send发送以下有效负载。

代码语言:javascript
复制
{
    "priority": "high",
    "to": "/topics/xx", // I tried this but I also tried to specify the device token received upon "registration" event. I did this using to:<device_token> and also using registration_ids: [<device_token>].
    "notification": {
      "title": "My Message",
      "body": "My Message Body",
      "badge": 1,
      "content-available": "1", // I tried with and without
      "category": "identifier", // I tried with and without
      "thread-id": "id", // I tried with and without
      "sound": "default",
      "icon": "default"
    },
    "data": {
        "title": "A short string describing the purpose of the notification",
        "body": "The text of the alert message",
        "clubId": 1000
    },
    "notId": 1,
    "custom_key1": "value1",
    "custom_key2": "value2"
}

注意:我尝试了与应用程序状态相关的所有可能的组合:后台的应用程序;应用程序的关闭;前台的应用程序;事件“通知”从未启动,推送通知也从未收到。

当我使用主题时,发送给FCM服务器的请求返回一个消息id (这是可以理解的,因为其他设备订阅了该主题)。因此,订阅相同主题的android会收到消息。另一方面,iOS却什么也得不到!

代码语言:javascript
复制
{
    "message_id": 5059997308576486332
}

如果我试图指定注册时收到的令牌,我将得到一个稍微不同的消息。大多数情况下,注册时接收的令牌工作正常,结果将包含一个字符串id。但这是暂时的,因为几分钟后,令牌变成了"NotRegistered“。

代码语言:javascript
复制
{
    "multicast_id": 88880398234xxxxx7,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [
        {
            "error": "NotRegistered" // <-- This happens after a few minutes. I have to delete the app and reinstall it in order to get a new token.
        }
    ]
}

这是构建配置

在我的设备上正确地启用了iOS通知。我少了什么?

更新:

  • 直接访问苹果的APN (是的.(没有FCM!) 我想通过FCM发送推送通知,但为了确定上述问题的原因,我决定直接尝试APN。为此,我必须从应用程序的config.xml中删除标记,这样phonegap-push-plugin就可以从APN而不是FCM获得令牌。

现在,使用新的令牌和使用node-apn模块与APN服务器通信的服务器,我可以向我的iOS应用程序发送推送通知。这样做的缺点是我失去了推动主题的能力,因为这是一个FCM唯一的特点。

我仍然不知道的唯一一件事是如何使用topic来锁定由push.subscribe()方法订阅的APN网络中的设备。

看看我的问题这里

在这个问题上也有帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-07 19:54:27

所以,原来推插件有一个问题。

这个问题只影响使用FCM的iOS设备应用程序的用户。如果你使用APN,它就能工作。

你可以在这里核对我的答案:https://github.com/phonegap/phonegap-plugin-push/issues/2644#issuecomment-445346335

我最初的报告是在这里:https://github.com/phonegap/phonegap-plugin-push/issues/2613

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53120736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档