繁体   English   中英

离子中的android推送通知

[英]android push notification in ionic

我正在使用ionic框架开发移动应用程序。我已经成功实现了推送通知。当我单击通知时,我希望它应该显示在特定页面上,我该怎么做? 在客户端:

 var user = $ionicUser.get();
     if(!user.user_id) {
     // Set your user_id here, or generate a random one.
     user.user_id = $ionicUser.generateGUID();
     };


     // Identify your user with the Ionic User Service
     $ionicUser.identify(user).then(function(){
     //$scope.identified = true;
     //alert('User ID ' + user.user_id);
     //alert("here");
     $ionicPush.register({
      canShowAlert: true, //Can pushes show an alert on your screen?
      canSetBadge: true, //Can pushes update app icon badges?
      canPlaySound: true, //Can notifications play a sound?
      canRunActionsOnWake: true, //Can run actions outside the app,
      onNotification: function(notification) {

        // Handle new push notifications here
        // console.log(notification);
        return true;
      }
     });
     });



  });
})

.config(['$ionicAppProvider', function($ionicAppProvider) {
  $ionicAppProvider.identify({
    app_id: 'xxxxxxxxxxxxx',
    api_key: 'xxxxxxxxxxxxxx',
    dev_push: false
  });
}])

在服务器端,我使用了php:

$deviceToken = 'APA91bHKwBKrIjmmZ9lke97fl_GbOQ9iRRo-S2sNnZp085hPqVaTHMOd0wqhYFF1PtrtOzFrETX7ZNIkU0JDhC49Tby_AEFIQFRX99B0QpZd7xdiTqsP6sZ08P-8ESdJAie5AJNxhW89nQ7S9evZNcAc9tsJaG91Xw';

$registrationIds = explode(",",$deviceToken);

//$registrationIds = array($deviceToken);

// prep the bundle

$msg = array

(

    'notId' => time(),

    'message'       => 'Technovault is awesome!!!',

    'title'         => 'Title',

    'smallIcon'     =>'icon'



);

$fields = array

(

    'registration_ids'  => $registrationIds,

    'data'              => $msg

);

$headers = array

(

    'Authorization: key=' . 'xxxxxxxxxx',

    'Content-Type: application/json'

);

$ch = curl_init();

curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );

curl_setopt( $ch,CURLOPT_POST, true );

curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );

curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );

curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );

$result = curl_exec($ch );

curl_close( $ch );

echo $result;

}

else {

    echo "Error";

}

我几天前解决了类似情况。 我有一个带有文章列表和文章详细信息的应用程序。 当有新文章时,服务器将发送通知,而不是在播放负载中包含文章ID。

event i can read this localStorage item and change the state of the app and show the article controller view for this article id. 当应用程序收到通知时,我将其保存在localStorage中,并且当用户单击通知时,通过事件,我可以读取此localStorage项目并更改应用程序的状态,并显示此文章ID的文章控制器视图。

library, i used the Cordova because i have a custom push notification server. 我没有使用库, 使用了Cordova 因为我有一个自定义的推送通知服务器。

应用收到通知后:

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
   if (ionic.Platform.isAndroid() && notification.event == 'message') {
        var newsid=notification.payload.newsid;
        if (typeof newsid != 'undefined') {
            // save the newsid to read it later in the resume event
            $window.localStorage.setItem('goafterpush',newsid);
        }
    }
});

event: 事件:

$ionicPlatform.on("resume",function(event){
     // read the newsid saved previously when received the notification
     var goafterpush=$window.localStorage.getItem('goafterpush');
     if (goafterpush) {
         $window.localStorage.removeItem('goafterpush');
         $state.go('app.newsdetail',{id:goafterpush});
     }
});

希望这段代码对您有所帮助。

我已经为Android和iOS工作,在python中,我已经使用状态名称和Id参数设置了有效负载

payload = {"$state":"state.name", "$stateParams": "{\"id\": \""+time()+"\"}"}

然后我将其包含在所有其他消息中

"payload": payload

这应该在$ msg数组内发生,仅在用户单击通知时您的应用程序打开并使用提供的参数进入定义的状态。

您在客户端收到的notification具有关联的参数foreground 当您通过单击通知进入应用程序时,它的值为false 因此,这是执行特定操作的方法:

onNotification: function(notification) {
                  if(notification.foreground == false){
                    //add your logic here to navigate to other page
                  }
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM