【问题标题】:How can I implement ionic io push notifications on Android如何在 Android 上实现 ionic io 推送通知
【发布时间】:2016-06-16 13:49:48
【问题描述】:

我在使用 ionic.io 和 phonegap-plugin-push 实现推送通知时遇到问题。 这是我的开发环境::

our system information:

Cordova CLI: 6.2.0
Gulp version: CLI version 3.9.1
Gulp local: 
Ionic Framework Version: 1.3.1
Ionic CLI Version: 1.7.15
Ionic App Lib Version: 0.7.2
OS: Distributor ID: LinuxMint Description: Linux Mint 17.1 Rebecca 
Node Version: v0.12.2

Installed platforms:
android 5.1.1

Testing Device:
Samsung Galaxy Core Prime LTE Android 4.4.4 (rooted)

所以我按照 www dot devdactic dot com/ionic-push-notifications-guide/ 上的教程进行操作 并尝试使用插件实现推送通知

ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push --variable SENDER_ID="1234567890"

现在我可以在我的浏览器中使用开发或演示推送通知。但是,当我尝试实现真正的推送通知并在设备上对其进行测试时,它不起作用。当应用程序启动时,它会按照教程中的预期成功生成设备令牌。但是,当我使用令牌为推送通知发送 CURL 或 Postman http 请求时,我得到一个 json 响应,就像我使用成功的 dev_push 通知(HTTP 状态 201)一样,但我从未在我的设备上收到实际的推送通知.我已经仔细检查了所有内容,例如确保 dev_push 设置为 false,我什至从头开始尝试了 3 次教程,以及 ionic 文档中的教程。在所有情况下,我都成功生成了一个开发令牌,并且我的 cURL 请求给出了肯定的响应,但设备上没有收到任何内容。

我的代码::

app.js:

angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {

    var push = new Ionic.Push({
      "debug": true
    });

    push.register(function(token) {
      console.log("Device token:",token.token);
      push.saveToken(token);  // persist the token in the Ionic Platform
    });

    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

index.html ---> http://pastebin.com/p9zwYXiQ

config.xml、package.json、postman 请求和响应 ---> http://pastebin.com/YT2Kn64W

【问题讨论】:

  • 你遇到了什么问题
  • 设备未收到通知。无论如何,我已经通过使用 One Signal 解决了这个问题。

标签: android angularjs cordova ionic-framework push-notification


【解决方案1】:

我在 Android 上得到了惊人的效果!离子糟糕的过时文档在这里并没有太大帮助。我最终创建了一个新应用程序,将 www 文件夹迁移到新应用程序,以避免麻烦。然后,除了以下内容之外,请确保执行 ionic 在文档中所说的内容:

从 Google Firebase 控制台创建一个新的 Firebase 帐户,是的,文档中从未提及。首次定义项目时,请确保包名称与 ionic 项目根目录中的 config.xml 中定义的相同。 Firebase 将为您提供一个新的项目 ID (SENDER_ID) 和一个 api 密钥。

更新 app.js 推送启动代码中的 SENDER_ID。

非常重要!!:Firebase 将指导您如何更新您的 gradle 项目,它们是 10 秒的更改,如果您不添加它们,您将不会收到通知!。

project_root --> 平台 --> android --> build.gradle:

// under buildscript { ... dependencies { ..
// paste this:
classpath 'com.google.gms:google-services:3.0.0'
// at the bottom of the file paste this
apply plugin: 'com.google.gms.google-services'

确保将这些行粘贴到正确的位置

在终端中运行 - 在此处使用您的新 SENDER_ID

cordova plugin add phonegap-plugin-push --variable SENDER_ID=SENDER_ID --save

app 更新 API 密钥 --> 证书 - 仅适用于 android!!

运行命令: // 重建离子平台文件

ionic build android

从您的真实设备运行应用程序,-c -l 用于终端中的 liveReaload 和日志记录控制台

ionic run android --device -l -c

祝你好运。

【讨论】:

    【解决方案2】:

    我一直在尝试,但不幸的是我从来没有成功过。我在这件事上浪费了整整一周的时间。幸运的是,我找到了一种可行且非常易于使用的替代方法。使用 OneSignal 推送通知:

    https://documentation.onesignal.com/docs/phonegap-sdk-setup

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多