简体   繁体   English

如何从离子移动应用程序发送/接收推送通知?

[英]How to send/receive push notifications from ionic mobile app?

I have a messaging app built using the Ionic framework (on cordova).我有一个使用 Ionic 框架(在cordova 上)构建的消息传递应用程序。 I plan on building this for android, and I'd like a way to send and recieve push notifications from the app using javascript/ionic.我计划为 android 构建这个,我想要一种使用 javascript/ionic 从应用程序发送和接收推送通知的方法。

Are there any good tutorials out there on how to go about setting something like this up?有没有关于如何设置这样的东西的好教程?

There is example application made available by Holly Schinsky. Holly Schinsky 提供了示例应用程序 The core of it is the usage of PushPlugin which is the standard method to handle push notifications on Cordova.它的核心是使用PushPlugin ,这是在 Cordova 上处理推送通知的标准方法。 There is quite extensive tutorial provided for this subject on their documentation on that GitHub repository.在该 GitHub 存储库的文档中为该主题提供了相当广泛的教程。 The main method is pushNotification.register which registers the device to listen for push notifications.主要方法是pushNotification.register ,它注册设备以侦听推送通知。

If you instead need to trigger notification locally, you might want to take a look at Local notification plugin instead.如果您需要在本地触发通知,则可能需要查看 本地通知插件 With it you can add notifications to be shown on the device without the need for external services to send the push notifications.有了它,您可以添加要在设备上显示的通知,而无需外部服务来发送推送通知。

Use this plugin https://github.com/phonegap-build/PushPlugin .使用这个插件https://github.com/phonegap-build/PushPlugin

Android devices receive push notifications through the Google Cloud Messaging (GCM) service, whereas iOS devices receive them from the Apple Push Notifications (APN) Service. Android 设备通过 Google Cloud Messaging (GCM) 服务接收推送通知,而 iOS 设备通过 Apple Push Notifications (APN) 服务接收推送通知。

The way the notifications are received (by sound, alert etc) is a combination of the options set in the application code upon registration as well as the user's device settings for notifications.接收通知的方式(通过声音、警报等)是注册时应用程序代码中设置的选项以及用户设备通知设置的组合。

If you want more specific follow below tutorial :如果你想要更具体的遵循以下教程:

http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/ http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

ngCordova has a plugin that supports Push Notifications. ngCordova 有一个支持推送通知的插件。 It has sample code for iOS and Android.它有适用于 iOS 和 Android 的示例代码。 Check it out: http://ngcordova.com/docs/plugins/pushNotifications/检查一下: http : //ngcordova.com/docs/plugins/pushNotifications/

The latest phonegap-plugin-push allows you to register and receive push notifications in your ionic apps.最新的 phonegap-plugin-push 允许您在您的 ionic 应用程序中注册和接收推送通知。 It is maintained at the following Github link:它在以下 Github 链接中维护:

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

Installation:安装:

cordova plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="XXXXXXX"

Where the XXXXXXX in SENDER_ID="XXXXXXX" maps to the project number in the Google Developer Console .XXXXXXXSENDER_ID="XXXXXXX"映射到该项目编号谷歌开发者控制台 To find the project number login to the Google Developer Console, select your project and click the menu item in the screen shot below to display your project number.要查找项目编号,请登录 Google Developer Console,选择您的项目并单击下面屏幕截图中的菜单项以显示您的项目编号。

zzns8

If you are not creating an Android application you can put in anything for this value.如果您不是在创建 Android 应用程序,则可以为此值输入任何内容。

Note: You may need to specify the SENDER_ID variable in your package.json.注意:您可能需要在 package.json 中指定 SENDER_ID 变量。

"cordovaPlugins": [
    {
      "variables": {
        "SENDER_ID": "XXXXXXX"
      },
      "locator": "phonegap-plugin-push"
    }
  ]

Note: You need to specify the SENDER_ID variable in your config.xml if you plan on installing/restoring plugins using the prepare method.注意:如果您计划使用准备方法安装/恢复插件,则需要在 config.xml 中指定 SENDER_ID 变量。 The prepare method will skip installing the plugin otherwise.否则,prepare 方法将跳过安装插件。

<plugin name="phonegap-plugin-push" spec="1.6.0">
    <param name="SENDER_ID" value="XXXXXXX" />
</plugin>

After installation you can now add code below to your main javascript file to register and receive push notifications:安装后,您现在可以将下面的代码添加到您的主要 javascript 文件中以注册和接收推送通知:

    $ionicPlatform.ready(function () {

         var push = PushNotification.init({
           android: {
             senderID: "XXXXXXX"//, //project token number (12 digit) from https://console.developers.google.com
             // forceShow: "true", //force show push notification when app is in foreground on Android only.
           },
           browser: {
             pushServiceURL: 'http://push.api.phonegap.com/v1/push'
           },
           ios: {
             /*senderID: "XXXXXXX",*/ //If using GCM for ios, project token number (12 digit) from https://console.developers.google.com
             /*gcmSandbox: 'true',*/ //If using GCM for ios
             alert: 'true',
             badge: 'true',
             sound: 'true',
           },
           windows: {}
         });

         PushNotification.hasPermission(function (permissionResult) {
           if (permissionResult.isEnabled) {
             $log.debug("has permission for push notification");

             /*Register device with GCM/APNs*/
             push.on('registration', function (data) {
               // data.registrationId
               $log.debug("data.registrationId: " + data.registrationId);          
             });

             push.on('notification', function (data) {
               // data.message,
               // data.title,
               // data.count,
               // data.sound,
               // data.image,
               // data.additionalData
               $log.debug(JSON.stringify(data));
             });

             push.on('error', function (e) {
               // e.message
               $log.debug("e.message: " + e.message);
               //alert(e.message);
             });
           }
         });
       }
     }

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

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