Ionic 3.x:iOS 上的推送通知不起作用(适用于 Android?)Ionic Native Plugin Push
Ionic 3.x: Push notification on iOS not working (works on Android?) Ionic Native Plugin Push
我正在使用/正在尝试使用 Ionic Native Push with Firebase Cloud Messaging。
(我相信)我正确安装了 Ionic Native Push Plugin。至少当我测试发送推送通知时它在 Android.
上工作
这是我的代码:
import { Injectable } from '@angular/core';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { AlertController } from 'ionic-angular';
@Injectable()
export class FcmProvider {
constructor(private push: Push,
private alertCtrl: AlertController) {
this.getPermission();
}
getPermission() {
this.initPush();
this.push.hasPermission().then((res: any) => {
if (res.isEnabled) {
console.log('We have permission to send push notifications');
} else {
console.log('We do NOT have permission to send push notifications');
this.presentErrorAlert();
}
});
}
private initPush() {
const options: PushOptions = {
android: {},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification);
this.presentSuccessAlert(notification.message);
});
pushObject.on('registration').subscribe((registration: any) => console.log('Device registered', registration));
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
private presentErrorAlert(): void {
let errorAlert = this.alertCtrl.create({
title: "Error",
subTitle: "Wir bekamen kein Token.",
buttons: ["Mies..."]
});
errorAlert.present();
}
private presentSuccessAlert(message: string): void {
let alert = this.alertCtrl.create({
title: "Neue Benachrichtigung",
message: message,
buttons: [
{
text: "Abbrechen",
role: "cancel",
handler: () => {
console.log("Cancel clicked");
}
},
{
text: "Ansehen",
handler: () => {
console.log("Show clicked");
}
}
]
});
alert.present();
}
}
当我使用 XCode 在 iOS 上进行调试时,我收到以下消息:
Push Plugin VoIP missing or false
Push Plugin register called
PushPlugin.register: setting badge to false
PushPlugin.register: clear badge is set to 0
PushPlugin.register: better button setup
FCM Sender ID <my-id>
Using FCM Notification
4.11.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) to your application initialization. Read more: <some-shortened-url>.
4.11.0 - [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40100000 started
4.11.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see <some-shortened-url>)
4.11.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud
-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.
FCM Registration Token: <my-token>
We have permission to send push notifications
Push Plugin register success: <some-id>
Device registered [object Object]
就我而言,一切似乎都很好。为什么推送通知不通过?
此外,这是我的系统的一些规格:
pdate: After some googling, here is additional information. Still can’t get it to work though!
- FirebaseInstanceID (2.0.10)
- FirebaseMessaging (2.0.8):
cli packages: (/usr/local/lib/node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 7.1.0
local packages:
@ionic/app-scripts : 3.1.8
Cordova Platforms : android 7.1.0 ios 4.5.4
Ionic Framework : ionic-angular 3.9.2
System:
ios-deploy : 1.9.2
ios-sim : 6.1.2
Node : v9.4.0
npm : 5.8.0
OS : macOS High Sierra
Xcode : Xcode 9.3 Build version 9E145
Environment Variables:
ANDROID_HOME : not set
Misc:
backend : pro
想通了!
我没有配置 iOS 键。
以下是此问题的未来访问者的说明:)
https://firebase.google.com/docs/cloud-messaging/ios/certs
首先,检查是否收到设计注册令牌,如果没有,则将 FirebaseAppDelegateProxyEnabled 添加到 YES,然后尝试通过邮递员发送推送通知
"notification":{
"title":"Notification title",
"body":"Notification body",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"data":{
"param1":"value1",
"param2":"value2"
},
"to":"<token>",
"priority":"high",
"restricted_package_name":""
}
如果出现 InvalidApnsCredential 错误,则问题不是您的插件
您必须添加(创建)具有推送通知权限的 Apple 开发密钥
https://developer.apple.com/account/ios/authkey/
和app id前缀
https://developer.apple.com/account/ios/identifier/bundle
到 Firebase iOS 应用:
Firebase 控制台 -> 项目 -> 设置 -> 云消息传递 -> iOS 应用配置
您可能无法将 @ionic-native/firebase 与 v3 of ionic 一起使用。
您需要直接使用 cordova-plugin-firebase。或者使用像 ionic-native-fcm
这样的插件
npm i ionic-native-fcm
我正在使用/正在尝试使用 Ionic Native Push with Firebase Cloud Messaging。
(我相信)我正确安装了 Ionic Native Push Plugin。至少当我测试发送推送通知时它在 Android.
上工作这是我的代码:
import { Injectable } from '@angular/core';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { AlertController } from 'ionic-angular';
@Injectable()
export class FcmProvider {
constructor(private push: Push,
private alertCtrl: AlertController) {
this.getPermission();
}
getPermission() {
this.initPush();
this.push.hasPermission().then((res: any) => {
if (res.isEnabled) {
console.log('We have permission to send push notifications');
} else {
console.log('We do NOT have permission to send push notifications');
this.presentErrorAlert();
}
});
}
private initPush() {
const options: PushOptions = {
android: {},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification);
this.presentSuccessAlert(notification.message);
});
pushObject.on('registration').subscribe((registration: any) => console.log('Device registered', registration));
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
private presentErrorAlert(): void {
let errorAlert = this.alertCtrl.create({
title: "Error",
subTitle: "Wir bekamen kein Token.",
buttons: ["Mies..."]
});
errorAlert.present();
}
private presentSuccessAlert(message: string): void {
let alert = this.alertCtrl.create({
title: "Neue Benachrichtigung",
message: message,
buttons: [
{
text: "Abbrechen",
role: "cancel",
handler: () => {
console.log("Cancel clicked");
}
},
{
text: "Ansehen",
handler: () => {
console.log("Show clicked");
}
}
]
});
alert.present();
}
}
当我使用 XCode 在 iOS 上进行调试时,我收到以下消息:
Push Plugin VoIP missing or false
Push Plugin register called
PushPlugin.register: setting badge to false
PushPlugin.register: clear badge is set to 0
PushPlugin.register: better button setup
FCM Sender ID <my-id>
Using FCM Notification
4.11.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) to your application initialization. Read more: <some-shortened-url>.
4.11.0 - [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40100000 started
4.11.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see <some-shortened-url>)
4.11.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud
-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.
FCM Registration Token: <my-token>
We have permission to send push notifications
Push Plugin register success: <some-id>
Device registered [object Object]
就我而言,一切似乎都很好。为什么推送通知不通过?
此外,这是我的系统的一些规格:
pdate: After some googling, here is additional information. Still can’t get it to work though!
- FirebaseInstanceID (2.0.10)
- FirebaseMessaging (2.0.8):
cli packages: (/usr/local/lib/node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 7.1.0
local packages:
@ionic/app-scripts : 3.1.8
Cordova Platforms : android 7.1.0 ios 4.5.4
Ionic Framework : ionic-angular 3.9.2
System:
ios-deploy : 1.9.2
ios-sim : 6.1.2
Node : v9.4.0
npm : 5.8.0
OS : macOS High Sierra
Xcode : Xcode 9.3 Build version 9E145
Environment Variables:
ANDROID_HOME : not set
Misc:
backend : pro
想通了! 我没有配置 iOS 键。 以下是此问题的未来访问者的说明:) https://firebase.google.com/docs/cloud-messaging/ios/certs
首先,检查是否收到设计注册令牌,如果没有,则将 FirebaseAppDelegateProxyEnabled 添加到 YES,然后尝试通过邮递员发送推送通知
"notification":{
"title":"Notification title",
"body":"Notification body",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"data":{
"param1":"value1",
"param2":"value2"
},
"to":"<token>",
"priority":"high",
"restricted_package_name":""
}
如果出现 InvalidApnsCredential 错误,则问题不是您的插件
您必须添加(创建)具有推送通知权限的 Apple 开发密钥 https://developer.apple.com/account/ios/authkey/
和app id前缀 https://developer.apple.com/account/ios/identifier/bundle
到 Firebase iOS 应用: Firebase 控制台 -> 项目 -> 设置 -> 云消息传递 -> iOS 应用配置
您可能无法将 @ionic-native/firebase 与 v3 of ionic 一起使用。
您需要直接使用 cordova-plugin-firebase。或者使用像 ionic-native-fcm
这样的插件npm i ionic-native-fcm