【问题标题】:Open a specific view in ionic app when user taps on push notification当用户点击推送通知时,在 ionic 应用程序中打开特定视图
【发布时间】: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


    【解决方案1】:

    如果您要向 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 对其进行编码并将其卷曲为离子。捕获是通知对象中的有效负载属性。 您需要添加有效负载属性

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

    【讨论】:

    • 嗨 Raj,我直接调用 FCM 的 REST api。无论如何,让我试一试。
    • 谢谢伙计。这是工作。但是我不知道为什么当我直接发送到 FCM 时它不起作用。可能会添加有效负载。一定会试一试的。
    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多