简体   繁体   English

如何在Cordova上获取Android本机推送通知?

[英]How to get android native push notification on cordova?

The event on application is being triggered when the server send the notification, but I get only the alert with the message on app, not the native notification. 当服务器发送通知时,将触发应用程序上的事件,但是我只收到应用程序上带有消息的警报,而不是本机通知。

I am using ionic framework with the $cordovaPush plugin. 我在$ cordovaPush插件中使用了离子框架。 On the server side I am using PushSharp library to call GCM. 在服务器端,我正在使用PushSharp库来调用GCM。

This is the code added to app.js on $ionicPlatform.ready 这是在$ ionicPlatform.ready上添加到app.js的代码

  var androidConfig = { badge: true, sound: true, alert: true, "senderID": "10xxxxxxxxxx" }; $cordovaPush.register(androidConfig).then(function (result) { // Success }, function (err) { // Error }) $rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) { alert('$cordovaPush:notificationReceived'); switch (notification.event) { case 'registered': if (notification.regid.length > 0) { alert('registration ID = ' + notification.regid); } break; case 'message': // this is the actual push notification. its format depends on the data model from the push server alert(JSON.stringify(notification)); break; case 'error': alert('GCM error = ' + notification.msg); break; default: alert('An unknown GCM event has occurred'); break; } }, false); 

This is the code from the server (C# + PushSharp lib) 这是来自服务器的代码(C#+ PushSharp库)

 var push = new PushBroker(); //Wire up the events for all the services that the broker registers push.OnNotificationSent += NotificationSent; push.OnChannelException += ChannelException; push.OnServiceException += ServiceException; push.OnNotificationFailed += NotificationFailed; push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired; push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged; push.OnChannelCreated += ChannelCreated; push.OnChannelDestroyed += ChannelDestroyed; push.RegisterGcmService(new GcmPushChannelSettings("AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); push.QueueNotification(new GcmNotification().ForDeviceRegistrationId( "APAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") .WithJson("{\\"alert\\":\\"Hello World!\\",\\"badge\\":7,\\"sound\\":\\"sound.caf\\"}")); 

I have not used that plugin, but I recommend you this one: 我没有使用过该插件,但我建议您使用以下插件:

https://github.com/phonegap/phonegap-plugin-push https://github.com/phonegap/phonegap-plugin-push

this is the one most used, and it works like a charm 这是最常用的一种,它就像一种魅力

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

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