当用户点击推送通知时在离子应用程序中打开特定视图

Open a specific view in ionic app when user taps on push notification

我知道这个问题已被多次询问和回答,但 none 的解决方案对我有用。

我已按照 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);
});

此代码在应用程序处于前台时运行良好。但是当应用程序关闭并且用户通过点击推送通知打开应用程序时,我想打开特定的 view/controller。 我将上面的代码放在 .运行 函数和 $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

ionic push插件有没有类似的功能?

如果您要向 ionic 发送 CURL 请求以进行推送,请使用此数据结构

$notficationHolder =array(
        "user_ids" => array(),
        "profile" => "push",
        "notification"=>array(
            "android"=>array(
                "title"=>'TITLE',
                "message"=>"You have a notification",
                "sound" => "sound",
                "icon_color" => "#FA2B2E",
                "icon" => "notification",   
                /* "image" => "https://pbs.twimg.com/profile_images/617058765167329280/9BkeDJlV.png", */
                "payload" => array(
                    '$state'=> 'main.myProfile',
                    '$stateParams'=> array()
                ) 
            ),

            "ios"=>array(
                "sound" => "default",
                "payload" => array(
                    '$state'=> 'main.myProfile',
                    '$stateParams'=> array()
                )
            )
        )
    );

这是一个数组。 json 对其进行编码并将其卷曲为离子。问题是通知对象中的负载 属性。 您需要添加 payload 属性

 {"$state":'yourStateName','$stateParams':{'paramOne':1,'paramTwo':2}}