【问题标题】:android push notification in ionic离子中的android推送通知
【发布时间】:2015-08-30 21:15:18
【问题描述】:

我正在使用离子框架开发移动应用程序。我已成功实现推送通知。当我点击通知时,我希望它应该进入特定页面。我将如何做到这一点? 在客户端:

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

}

【问题讨论】:

  • 请用您的代码更新您的问题。

标签: android ionic


【解决方案1】:

您在客户端收到的notification 具有关联的参数foreground。当您从点击通知进入应用程序时,它的值将是false。所以这就是您可以执行特定操作的方式:

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

【讨论】:

  • 嗨,我仍然不知道解决方案...我正在使用节点服务器,并将有效负载像这样放在我的服务器中:“notification”:{“alert”:message.message, ios": {"badge":1,"payload": {"taskId": message.relatedTask } },但我不知道如何在我的客户端中读取我的 taskId 有效负载。您将如何在 onNotification 回调中读取有效负载?
  • @HughHou 如果您使用已弃用的旧插件,那么您可以通过notification.payload.taskId 的方式在通知对象的客户端访问 taskId。但是如果你使用的是最新的插件,那么notification.additionalData.taskId .
【解决方案2】:

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

当应用程序收到通知时,我将其保存在 localStorage 中,当用户单击通知时,通过 resume 事件,我可以读取此 localStorage 项目并更改应用程序的状态和显示此文章 ID 的文章控制器视图。

我没有使用 ionPush 库,我使用了 Cordova PushPlugin,因为我有一个自定义推送通知服务器。

应用收到通知时:

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

还有 resume 事件:

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

希望这段代码可以帮到你。

【讨论】:

    【解决方案3】:

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

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

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

    "payload": payload
    

    这应该发生在您的 $msg 数组中,就是这样,当用户单击您的应用打开的通知并使用提供的参数进入定义的状态时。

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多