在离子的Andr​​oid推送通知 [英] android push notification in ionic

查看:181
本文介绍了在离子的Andr​​oid推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用离子framework.I移动应用程序已经成功实施推notification.When我点击我想它应该在特定的page.How我会做这个通知?
在客户端:

  VAR用户= $ ionicUser.get();
     如果(!user.user_id){
     //这里设置你的user_id,或生成一个随机的。
     user.user_id = $ ionicUser.generateGUID();
     };
     //与离子用户服务识别用户
     $ ionicUser.identify(用户)。然后(函数(){
     //$scope.identified = TRUE;
     //警报(用户ID'+ user.user_id);
     //警报(这里);
     $ ionicPush.register({
      canShowAlert:真,//灿推显示在屏幕上的警报?
      canSetBadge:真,//灿推动更新的应用程序图标徽章?
      canPlaySound:真,//可以通知播放声音?
      canRunActionsOnWake:真,//可以应用在外面跑的动作,
      onNotification:功能(通知){        //处理新的推送通知这里
        //执行console.log(通知);
        返回true;
      }
     });
     });  });
})的.config(['$ ionicAppProvider',函数($ ionicAppProvider){
  $ ionicAppProvider.identify({
    APP_ID:XXXXXXXXXXXXX',
    API_KEY:XXXXXXXXXXXXXX',
    dev_push:假的
  });
}])

在服务器端我用PHP的:

  $ deviceToken = 'APA91bHKwBKrIjmmZ9lke97fl_GbOQ9iRRo-S2sNnZp085hPqVaTHMOd0wqhYFF1PtrtOzFrETX7ZNIkU0JDhC49Tby_AEFIQFRX99B0QpZd7xdiTqsP6sZ08P-8ESdJAie5AJNxhW89nQ7S9evZNcAc9tsJaG91Xw';$ registrationIds =爆炸(,,$ deviceToken);// $ registrationIds =阵列($ deviceToken);// preP包$味精=阵列(    notId'=>时间(),    '消息'= GT; Technovault是真棒!,    '标题'=> '标题',    smallIcon'=>'图标);$栏=阵列(    registration_ids'=> $ registrationIds,    '数据'=> $味精);$标题=阵列(    授权:键='。 'XXXXXXXXXX',    内容类型:应用程序/ JSON);$ CH = curl_init();curl_setopt($ CH,CURLOPT_URL,'https://android.googleapis.com/gcm/send');curl_setopt($ CH,CURLOPT_POST,真正的);curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($域));$结果= curl_exec($ CH);curl_close($ CH);回声$结果;}其他{    回声错误;}


解决方案

的通知您收到客户端上有参数前景相关。它的值将是当你来到从点击通知应用程序。因此,这是你如何执行特定操作:

  onNotification:功能(通知){
                  如果(notification.foreground == FALSE){
                    //添加你的逻辑在这里浏览其他页面
                  }
                }

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? 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:

$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";

}

解决方案

notification you receive on client side has parameter foreground associated. It's value will be false when you come to application from clicking notification. 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
                  }
                }

这篇关于在离子的Andr​​oid推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆