Cordova 使用Ionic Native Push为Firebase云消息传递获取正确的FCM注册令牌
我在检索正确的FCM注册令牌以向Ionic 3应用程序中的特定设备发送推送通知时遇到问题 我已经尝试过实现示例,如、和。然而,当我尝试检索令牌时,所有的示例似乎都给了我相同的注册令牌。当我尝试使用Firebase云消息发送到特定设备时(指定从每个API检索到的FCM注册令牌,并提供该令牌),消息失败 但是,我可以发送到整个设备。我已经看到,用户似乎有类似的问题,以及使用主题作为解决方法的解决方案 我的测试过程如下: 1.删除当前平台并使用添加新的更新平台Cordova 使用Ionic Native Push为Firebase云消息传递获取正确的FCM注册令牌,cordova,android-studio,push-notification,ionic3,firebase-cloud-messaging,Cordova,Android Studio,Push Notification,Ionic3,Firebase Cloud Messaging,我在检索正确的FCM注册令牌以向Ionic 3应用程序中的特定设备发送推送通知时遇到问题 我已经尝试过实现示例,如、和。然而,当我尝试检索令牌时,所有的示例似乎都给了我相同的注册令牌。当我尝试使用Firebase云消息发送到特定设备时(指定从每个API检索到的FCM注册令牌,并提供该令牌),消息失败 但是,我可以发送到整个设备。我已经看到,用户似乎有类似的问题,以及使用主题作为解决方法的解决方案 我的测试过程如下: 1.删除当前平台并使用添加新的更新平台 ionic cordova platfo
ionic cordova platform remove android && ionic cordova platform add
android
2.在android studio中打开平台文件夹。添加googleservices.json文件夹以与Firebase集成
3.同步gradle构建并在emulator上运行它(供参考,Nexus 5X API 28)。使用铬合金上的检查装置(这是chrome://inspect/#devices)查看注册ID
4.尝试使用Firebase Cloud Messaging发送使用registrationId的测试消息。它遵循以下格式:“eJAPP91Mq_Q:APA91b…”,长度为174个字符
5.测试消息失败
6.尝试发送相同的消息,但定向到Firebase云消息中的所有设备。消息成功并显示在仿真器上
以下是发布在app.component.ts
中的Ionic中的相关代码
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { HomePage } from '../pages/home/home';
import { FCM } from '@ionic-native/fcm';
import { Push, PushObject, PushOptions } from "@ionic-native/push";
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = HomePage;
constructor(public platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,
private fcm: FCM, public push: Push ) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
this.registerPush();
});
}
registerPush() {
const options: PushOptions = {
android: {
senderID: 'xxxxxxxxxxxxx'
},
ios: {
alert: 'true',
badge: true,
sound: 'false'
}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) =>
{
console.log('Received a notification', notification)
});
pushObject.on('registration').subscribe((registration: any) => {
console.log('Device registered', registration)
});
pushObject.on('error').subscribe(error => {
console.error('Error with Push plugin', error)
});
}
}
需要的相关软件包/插件(来自Ionic Native Push文档)
Cordova插件:phonegap插件推送
$ ionic cordova plugin add phonegap-plugin-push
$ npm install --save @ionic-native/push
IDE:Webstorm
到目前为止,我已经检查了:
SenderID:如果Firebase发送给所有用户,它可以接收消息,所以我怀疑这是问题所在
包名称问题:见上文
可能错误的令牌:通过多种方法获取生成相同设备令牌的令牌,似乎可以证明这一点
如果你需要更多信息,请告诉我!
谢谢。编辑:原来,phone gap插件不支持模拟器。然而,它确实适用于发送到所有未解释的设备的消息。我的理论是,这是因为它发送了一种不同类型的负载,符合插件的期望 我发现我的代码适用于一个真正的设备(由我的朋友提供),并且主题方法也适用,所以我将此标记为关闭