【问题标题】:How to get android native push notification on cordova?如何在cordova上获得android原生推送通知?
【发布时间】:2015-12-15 11:26:25
【问题描述】:

服务器发送通知时正在触发应用程序上的事件,但我只收到应用程序上消息的警报,而不是本机通知。

我正在使用带有 $cordovaPush 插件的 ionic 框架。在服务器端,我使用 PushSharp 库来调用 GCM。

这是在 $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);

这是来自服务器的代码(C# + PushSharp lib)

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\"}"));

【问题讨论】:

    标签: android cordova notifications push pushsharp


    【解决方案1】:

    我没有用过那个插件,但我推荐你这个:

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

    这是最常用的一个,它就像一个魅力

    【讨论】:

    • 你,伙计,拯救了我的一天。现在,我在应用程序关闭时收到通知。非常感谢!! :)
    • 是的,发生了这种情况。当应用程序不在前面时,您只会在栏中收到通知。如果是,则启动 onNotification 功能但不显示在栏中。很高兴为您提供帮助,您可以投票或批准答案;)
    猜你喜欢
    • 2021-05-13
    • 2017-12-18
    • 2016-05-29
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多