用户代理:Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 60.0.3112.113 Safari浏览器/ 537.36
当应用程序处于打开状态时,它可以正常运行接收通知,它会自动重定向到特定页面。但是当应用程序关闭时&我点击通知,它没有重定向到页面。它只启动应用程序。
通知代码上的pushObject
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification);
if(notification.additionalData.type=="news"){
this.NewsAndEventsDetails(notification.additionalData.type_id);
}else if(notification.additionalData.type=="notice"){
this.getNoticesSingle(notification.additionalData.type_id);
}else if(notification.additionalData.type=="bill"){
this.getBillsSingle(notification.additionalData.type_id);
}else{
let prompt = this.alertCtrl.create({
title: notification.title,
message: notification.message
});
prompt.present();
}
});
答案 0 :(得分:0)
从我的角度来看,同样的问题发生了,
我在执行以下步骤时解决了这个问题。
Install FCM plugin
ionic cordova plugin add cordova-plugin-fcm
npm install --save @ionic-native/fcm
Get google-services.json file GoogleService-Info.plist file from fcm for your registered project.
Add google-services.json in android platform root like /platforms/android/google-services.json
Add GoogleService-Info.plist file in ios platform like /patforms/ios/ProjectName/Resoures/Resoures/GoogleService-Info.plist
or also add at /platforms/android/GoogleService-Info.plist
Now run the project on android and ios device you get fcmid.
Store fcmid to the server.
Server side code is like this.
public function sendNotification($sectionName){
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'to' => "fcmId",
'notification' => array (
"body" => "Hello test",
"title" => "Title",
"icon" => "myicon",
"sound" => "default",
"click_action" => "FCM_PLUGIN_ACTIVITY", // this is important for ontapped
),
'data' => array('rollno' => "test",
'name' => "manish",
'activitySection' => $sectionName),
'priority' => 'high',
);
$fields = json_encode ( $fields );
echo $fields;
$headers = array (
'Authorization: key=' . "fcm server key",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
}
This is on app
fcmInitilization() {
if (!this.plt.is('cordova')) {
console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
return;
}
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
// do onTapped work here means app in background
} else {
let confirmAlert = this.alertCtrl.create({
title: 'New Notification',
message: data.title,
buttons: [{
text: 'Ignore',
role: 'cancel'
}, {
text: 'View',
handler: () => {
//TODO: Your logic here
}
}]
});
confirmAlert.present();
};
})
}
答案 1 :(得分:0)
仅在通知订阅之前读取“ this”指针的实例:
let self = this
然后将代码中的每个“ this”替换为“ self”,例如:
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification);
if(notification.additionalData.type=="news"){
self.NewsAndEventsDetails(notification.additionalData.type_id);
}else if(notification.additionalData.type=="notice"){
self.getNoticesSingle(notification.additionalData.type_id);
}else if(notification.additionalData.type=="bill"){
self.getBillsSingle(notification.additionalData.type_id);
}else{
let prompt = this.alertCtrl.create({
title: notification.title,
message: notification.message
});
prompt.present();
}
});
答案 2 :(得分:0)
如果使用FMC程序包,则很简单: 从服务器端,您发送通知和像这样的数据格式:
$fields = array();
$fields['notification'] = array(
'title' => $title,
'body' => $message,
'icon' => $icon,
'click_action' => $action
);
$fields['to'] = $subscription_ids;
$fields_data = json_encode($fields);
并通过curl发送。
安装FCM软件包后,在前端只需在根目录中创建一个名称为sw.js的文件,并在下面的代码中进行以下操作:
self.addEventListener('notificationclick', function(event) {
var url = './index.html';
event.notification.close(); //Close the notification
// Open the app and navigate to latest.html after clicking the notification
event.waitUntil(
clients.openWindow(url)
);
});
希望它能工作