简体   繁体   English

离子中的android推送通知

[英]android push notification in ionic

I am developing mobile app using ionic framework.I have Successfully implemented push notification.When I click on the notification I want it should go on particular page.How will I do this? 我正在使用ionic框架开发移动应用程序。我已经成功实现了推送通知。当我单击通知时,我希望它应该显示在特定页面上,我该怎么做? In Client Side: 在客户端:

 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
  });
}])

In Server Side I have used php: 在服务器端,我使用了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";

}

I resolve a similar situation some days ago. 我几天前解决了类似情况。 I've an app with articles list and article details. 我有一个带有文章列表和文章详细信息的应用程序。 When there is a new article the server send a notification than include the article id in the playload. 当有新文章时,服务器将发送通知,而不是在播放负载中包含文章ID。

When the app receive the notification, i save it in the localStorage and when the user click on the notification, with the resume 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中,并且当用户单击通知时,通过resume事件,我可以读取此localStorage项目并更改应用程序的状态,并显示此文章ID的文章控制器视图。

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

When the app received the notification: 应用收到通知后:

$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);
        }
    }
});

And the resume 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});
     }
});

I hope this code can help you. 希望这段代码对您有所帮助。

I've this working for Android and iOS, in python I've set payload with the state name and Id parameter 我已经为Android和iOS工作,在python中,我已经使用状态名称和Id参数设置了有效负载

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

Then I include it with all the other message stuff 然后我将其包含在所有其他消息中

"payload": payload

This should happen inside your $msg array, and that's it, when the user clicks the notification your app opens and goes to the defined state using the provided parameter. 这应该在$ msg数组内发生,仅在用户单击通知时您的应用程序打开并使用提供的参数进入定义的状态。

notification you receive on client side has parameter foreground associated. 您在客户端收到的notification具有关联的参数foreground It's value will be false when you come to application from clicking notification. 当您通过单击通知进入应用程序时,它的值为false So this is how you can perform specific action: 因此,这是执行特定操作的方法:

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