【发布时间】:2016-09-06 11:35:07
【问题描述】:
我知道这个问题已被多次询问和回答,但没有一个解决方案适合我。
我已按照 ionic.io 文档 https://docs.ionic.io/services/push/ 实现推送通知,并且当应用程序处于前台时它工作正常。当应用程序关闭时,我也能够收到通知。现在,当用户点击该通知时,我想打开一个特定的视图。
根据文档,要在您的应用中处理推送通知,我们需要使用 Angular 的 $on 监听 cloud:push:notification 事件。
$scope.$on('cloud:push:notification', function(event, data) {
var msg = data.message;
alert(msg.title + ': ' + msg.text);
});
当应用程序处于前台时,此代码可以正常工作。但是当应用程序关闭并且用户通过点击推送通知打开应用程序时,我想打开特定的视图/控制器。 我已将上述代码放在 .run 函数和 $ionicPlatform.ready 函数之外。 这是我调用 FCM Rest 服务的代码
function sendFCMNotification($request_data){
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
//The device token.
$token = $request_data['device_id'];
//Title of the Notification.
$title = $request_data['title'];
//Body of the Notification.
$body = $request_data['body'];
//Creating the notification array.
$notification = array('title' =>$title , 'body' => $body,'content-available'=> '1');
//This array contains, the token and the notification. The 'to' attribute stores the token.
$arrayToSend = array('to' => $token, 'notification' => $notification);
//Generating JSON encoded string form the above array.
$json = json_encode($arrayToSend);
//Setup headers:
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key= {MY GCM KEY}';
//Setup curl, add headers and post parameters.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
curl_exec($ch);
//Close request
curl_close($ch);
}
谁能帮我实现这个目标?
Pushwoosh 提供了一种方法,可以通过点击推送通知告诉我们应用程序是否已启动。 https://rawgit.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin/master/Documentation/files/PushNotification-js.html#PushNotification.getLaunchNotification
离子推送插件有没有类似的功能?
【问题讨论】:
标签: android cordova ionic-framework push-notification phonegap-plugins