我创建了一个示例Cordova应用程序,它使用“ phonegap-push-plugin”。 该应用程序没有任何复杂性。在“ deviceready”上,我运行插件初始化代码,如下所示:
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)
这是控制台的日志输出:
=> 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
为了进行推送,我将以下负载发送到旧版服务器https://fcm.googleapis.com/fcm/send
。
{
"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什么也收不到!
{
"message_id": 5059997308576486332
}
如果我尝试指定注册时收到的令牌,则会收到一条略有不同的消息。在大多数情况下,注册时收到的令牌有效,并且结果将包含字符串ID。但这是暂时的,因为几分钟后令牌变为“未注册”。
{
"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设备上正确启用。我想念什么?
已更新:
phonegap-push-plugin
可以从APN而不是从FCM获取令牌。现在,使用新令牌和使用node-apn
模块与APN服务器通信的服务器,我可以向我的iOS应用发送推送通知。缺点是,由于这是FCM only feature.
我唯一仍然不知道的是如何使用topic
来定位APN网络中通过push.subscribe()方法订阅的设备。
查看我的问题here。
对此也有帮助吗?
答案 0 :(得分:0)
因此,事实证明推插件存在问题。
此问题仅影响使用FCM的iOS设备应用程序上的用户。 如果您使用APN,则可以使用。
您可以在这里查看我的答案:https://github.com/phonegap/phonegap-plugin-push/issues/2644#issuecomment-445346335
我的第一个问题在这里报告: https://github.com/phonegap/phonegap-plugin-push/issues/2613