设备收到 GCM Android 通知但未显示

2024-05-03

尽管通知已在应用程序本身中注册,但我的 Ionic Android 应用程序的 GCM Cloud 消息通知未出现在我的设备的主屏幕中。

我正在使用 npm 模块node-gcm https://www.npmjs.com/package/node-gcm发送推送通知。

var gcm = require('node-gcm');

var message = new gcm.Message({
    priority: 'high',
    contentAvailable: true,
    delayWhileIdle: true,
    timeToLive: 10,
    dryRun: false,
    data: {
        key1: 'message1',
        key2: 'message2'
    },
    notification: {
        title: "Hello, World",
        body: "This is a notification that will be displayed ASAP."
    }
});
var regIds = ['*device id*'];

var sender = new gcm.Sender('*api key*');

sender.send(message, { registrationIds: regIds }, function (err, result) {
    if(err) console.error(err);
    else console.log(result);
});

当我向我的设备 ID 发送推送通知时,我收到了成功的响应:

{ multicast_id: 8406385547051869000,
  success: 1,
  failure: 0,
  canonical_ids: 0,
  results: [ { message_id: '0:1441962697347777%b67ee170f9fd7ecd' } ] }

然后我在 Android Studio 控制台中收到以下消息:

V/GCMBroadcastReceiver﹕ onReceive: com.google.android.c2dm.intent.RECEIVE
V/GCMBroadcastReceiver﹕ GCM IntentService class: com.plugin.gcm.GCMIntentService
V/GCMBaseIntentService﹕ Acquiring wakelock
V/GCMBaseIntentService﹕ Intent service name: GCMIntentService-GCMIntentService-5
D/GCMIntentService﹕ onMessage - context: android.app.Application@2dc6dbff
V/GCMBaseIntentService﹕ Releasing wakelock

在 Google Play 开发者控制台 GCM 调试器中,我的通知似乎也已得到确认。

0: 1441899623073525% b67ee170f9fd7ecd Confirmed

除此之外,当收到通知时,我在 Android Studio 控制台中没有收到任何错误消息。

一旦我发送通知,Ionic 应用程序本身就会注册通知。但是当我退出应用程序时。主屏幕上不显示任何通知。

$rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
    alert(notification);
    if (ionic.Platform.isAndroid() && notification.event == "registered") {
            window.localStorage['token'] = notification.regid;
            var params = {
              deviceType: 'android',
              tokenId: notification.regid
            };
            NotificationService.registerDevice(params);
          }

          if (notification.badge) {
            $cordovaPush.setBadgeNumber(notification.badge);
          }

          //notifications payload
          if (notification.foreground == '0') {
            if (notification.view) {
              $timeout(function () {
                $state.go('app.notifications');
              });
            } else {
              if (notification.id) {
                   NotificationService.markNotificationAsRead(notification.id).success(function () {
                  $rootScope.$emit('notifications-read');
                });
              }
              if (notification.chat) {
                $timeout(function () {
                  $state.go('app.message', {id: notification.chat});
                });
              } else if (notification.post) {
                $timeout(function () {
                  $state.go('app.singlePost', {id: notification.post});
                });
              } else if (notification.group) {
                $timeout(function () {
                  $state.go('app.group', {id: notification.group});
                });
              }
            }
          }
    });

您必须在您的notification block:

notification: {
    title: "Hello, World",
    body: "This is a notification that will be displayed ASAP.",
    icon: "@drawable/ic_launcher"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

设备收到 GCM Android 通知但未显示 的相关文章

随机推荐

  • 如何在目标c中获取当前位置的纬度和经度

    我使用以下代码来获取当前位置 我添加了 corelocation 框架 void viewDidLoad super viewDidLoad locationManager CLLocationManager alloc init loca
  • ASP.NET 的电子邮件地址验证

    使用什么来验证 ASP NET 表单上的电子邮件地址 我想确保它不包含 XSS 漏洞 这是 ASP NET 1 1 ASP NET Web 表单上发布的任何脚本标记都会导致您的网站抛出未处理的异常 您可以使用 asp 正则表达式验证器来确认
  • 为 Keras 编写自定义数据生成器

    我将每个数据点存储在 npy 文件中 其中shape 1024 7 8 我想通过类似的方式将它们加载到 Keras 模型中ImageDataGenerator 所以我编写并尝试了不同的自定义生成器 但它们都不起作用 这是我改编的一个this
  • 正确别名向量

    我无法在其他地方找到答案 所以我想我只需要问这个 我正在尝试获取向量 其中存储 int 指针 的别名 如下所示 void conversion Engine ENGINES The Engine class has a vector of
  • 创建带小数秒的时间戳

    awk可以使用 strftime 函数生成时间戳 例如 awk BEGIN print strftime Y m d H M S 2019 03 26 08 50 42 但我需要一个带有小数秒的时间戳 最好是纳秒 gnu date可以用 N
  • 如何使用 swift 隐藏导航控制器中的后栏按钮

    在故事板 Xcode 6 iOS 8 和 swift 中 我在导航控制器中嵌入了 TableViewController 从对象库中 我拖放一个栏按钮项目作为后退按钮 它显示一个图标图像 当我单击该按钮时 我显示一个设置视图 我怎样才能隐藏
  • 集成共享同一个 MySQL 数据存储的 Django 和 Rails 应用程序的最佳方式是什么?

    我将在网络上与 Python 开发人员合作 应用 我将用 Ruby 构建其中的一部分 而他正在 将使用 Django 构建它的另一部分 我不太了解 姜戈 我集成这两部分的计划是简单地映射某个 URL Python 的路径前缀 例如 以 se
  • Android GLSurfaceView 具有可绘制背景

    我有一个带有可绘制对象作为背景的 GLSurfaceView 但是在没有 surfaceView setZOrderOnTop true 的情况下渲染时只有背景可见 我需要避免使用 setZOrderOnTop true 因为在 GLSur
  • 模型绑定到 MVC 中的列表

    我无法在服务器端检索简单的列表 有人能指出我正确的方向吗 public class TestList public string id get set public string name get set public string loc
  • 如何在 Xamarin.Forms 中强制使用浅色模式?

    我的应用程序的 UI 设计为在灯光模式下使用 但如果手机的默认主题是深色模式 我的应用程序也会切换到深色模式 并且 UI 看起来很垃圾 所以我想强制我的应用程序使用灯光模式 我怎样才能做到这一点 In my app xaml我使用的文件Us
  • __subclasses__ 没有显示任何内容

    我正在实现一个从适当的子类返回对象的函数 如果我搬家SubClass from base py 没有出现子类 subclasses 它们必须在同一个文件中吗 也许我从来没有直接导入subclass py对Python隐藏子类 我能做些什么
  • 使用类型作为参数 Typescript

    如何在 Typescript 中使用类型作为参数 type myType const passingType t Type gt const x t passingType myType 我收到 TS 错误 t 指的是一个值 但在这里被用作
  • 在 jqgrid 的 0 行上,我们如何将 NaN 的第 1 页替换为其他内容?

    如果 jqgrid 在某个时间没有行 它会显示Page 1 of NaN什么是Nan这里 我们不能把它改成更合适的东西吗Page 0 of 0或者更好的东西 我的 jqgrid 代码 var grid jQuery list1 grid j
  • 为什么 C++11/Boost `unordered_map` 在擦除时不重新散列?

    我想知道为什么 C 11 和 Boost 的 hashmap 在通过迭代擦除元素时不会调整大小 即使这在技术上不是内存泄漏 我认为这可能是应用程序中的一个严重问题 这对我来说是一个隐藏的问题 很难追踪它 并且它实际上可能会影响许多应用程序
  • 如何在 spring-boot 中禁用 spring-data-mongodb 自动配置

    有没有人尝试过在 spring boot 中禁用 mongodb 的自动配置 我正在尝试使用 spring data mongodb 来启动 spring boot 使用基于java的配置 使用 spring boot 1 2 1 RELE
  • Python、cPickle、酸洗 lambda 函数

    我必须像这样腌制一组对象 import cPickle as pickle from numpy import sin cos array tmp lambda x sin x cos x test array tmp tmp tmp tm
  • Proguard错误android,无法访问jar文件

    这是我第一次使用 progurad 在导出签名的 apk 时出现以下错误 错误 无法访问 jarfile lib proguard jar 我取消注释了 proguard config 行 To enable ProGuard to shr
  • Qml 中的 FileDialog 在发布中不起作用

    我正在与以下项目合作Qt Quick Control 2 当我尝试在调试模式下运行软件时 FileDialog qml 可以完美打开 但是当我将其部署为发布模式时 它无法工作 这是我的代码 import QtQuick 2 4 import
  • 在 PHP 中查找数字的倍数

    我想在 PHP 中找到一个数字的所有倍数 我正在使用这样的东西 if count 20 计算出如果 count不等于20 但我还需要这个脚本来检查是否 count不等于 20 40 60 80 100 120 140 160 等 有任何想法
  • 设备收到 GCM Android 通知但未显示

    尽管通知已在应用程序本身中注册 但我的 Ionic Android 应用程序的 GCM Cloud 消息通知未出现在我的设备的主屏幕中 我正在使用 npm 模块node gcm https www npmjs com package nod