0

使用 Angular 和 Ionic 创建 UI,并使用包装器使用 Phonegap,如何在 Android 中实现推送通知。Phonegap 中是否有任何有效且准确的库来实现 Pushnotification。

已经在使用https://github.com/phonegap-build/PushPlugin,但是遇到了一些问题,例如未收到推送通知,有时所有通知都会同时出现。

4

4 回答 4

2
  1. 首先在 Google 控制台中创建一个项目
  2. 启用 GCM
  3. 创建服务器 api Key

使用以下插件

cordova plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX"

将 xxxxxx 替换为您的发件人 ID

在您的 javascript 添加以下代码以注册到 GCM 服务器,它将为您提供 GCM id

   var push = PushNotification.init({
android: {
    senderID: "XXXXXXXX" //add your sender id here
},
ios: {
    alert: "true",
    badge: "true",
    sound: "true"
},
windows: {}
});

push.on('registration', function(data) {   
    consol.log(data.registrationId);  //this function give registration id from the GCM server if you dont want to see it please comment it
    document.getElementById("gcm_id").value= data.registrationId; //showing registration id in our app. If it shows our registration process is suscess
    //$("#gcm_id").val(data.registrationId);  if you are using jquery
});

使用 GCM HTTP 连接服务器协议发送消息:

https://gcm-http.googleapis.com/gcm/send

Content-Type:application/json
  Authorization:key=YOUR SERVER KEY
  {
    "to": "GCM ID",
    "data": {
      "message": "This is a GCM Topic Message!",
     }
  }

更多细节..

http://phonegaptut.com/2016/05/31/how-to-send-push-notifications-in-phonegap-application/

于 2016-06-15T17:08:48.427 回答
1

我最近遇到了同样的问题,使用 Ionic 和 Cordova(不是 Phonegap,但应该可以工作)。我最终将这个库用于本地推送通知https://github.com/Wizcorp/phonegap-plugin-localNotifications

他们工作得很好,除了 PN 不会在 Android 上启动应用程序,但我打开了一个拉取请求并修复了. 如果你遇到同样的问题,你也可以使用我的这个插件的分支,它已经包含了这个修复。

于 2015-02-02T07:20:20.077 回答
1

您应该为此使用ngCordova 的 Push Plugin

从文档:

允许您的应用程序接收推送通知。要在您的控制器或服务中接收通知,请侦听 pushNotificationReceived 事件。

于 2015-02-02T14:28:11.460 回答
1

由于 Push Notification 是一项原生功能,因此要将 Push 集成到 PhoneGap Android 应用程序中,您必须制作一个与 Android Native 代码通信的插件。

您可以使用 Git Hub 上的示例应用程序。还请按照自述文件中提到的必要步骤进行操作。

于 2015-02-20T10:03:15.657 回答