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

时间:2016-09-06 11:35:07

标签: android cordova ionic-framework push-notification phonegap-plugins

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

我已经关注了ionic.io文档https://docs.ionic.io/services/push/来实现推送通知,当app在前台时它可以正常工作。我还能够在应用关闭时收到通知。现在,当用户点击该通知时,我想打开一个特定的视图。

根据文档,要在您的应用中处理推送通知,我们需要使用angular的$ on来监听云: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

离子推送插件中是否有类似的功能?

1 个答案:

答案 0 :(得分:1)

如果要将CURL req发送到ionic,请使用此数据结构

$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编码并将其卷曲成离子。 catch是通知对象中的有效内容属性。 您需要添加有效内容属性

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