推送通知未显示

2024-04-12

我开发了一个使用推送插件的phonegap应用程序。我创建了一个示例服务器,推送消息未显示在通知栏中,但消息正在应用程序中显示。请帮忙

我的代码是

var 推送通知;

        function onDeviceReady() {
            $("#app-status-ul").append('<li>deviceready event received</li>');

            document.addEventListener("backbutton", function(e)
            {
                $("#app-status-ul").append('<li>backbutton event received</li>');

                if( $("#home").length > 0)
                {
                    // call this to get a new token each time. don't call it to reuse existing token.
                    //pushNotification.unregister(successHandler, errorHandler);
                    e.preventDefault();
                    navigator.app.exitApp();
                }
                else
                {
                    navigator.app.backHistory();
                }
            }, false);

            try 
            { 
                pushNotification = window.plugins.pushNotification;
                if (device.platform == 'android' || device.platform == 'Android') {
                    $("#app-status-ul").append('<li>registering android</li>');
                    pushNotification.register(successHandler, errorHandler, {"senderID":"599322667326","ecb":"onNotificationGCM"});     // required!
                } else {
                    $("#app-status-ul").append('<li>registering iOS</li>');
                    pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});    // required!
                }
            }
            catch(err) 
            { 
                txt="There was an error on this page.\n\n"; 
                txt+="Error description: " + err.message + "\n\n"; 
                alert(txt); 
            } 
        }

        // handle APNS notifications for iOS
        function onNotificationAPN(e) {
            if (e.alert) {
                 $("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>');
                 navigator.notification.alert(e.alert);
            }

            if (e.sound) {
                var snd = new Media(e.sound);
                snd.play();
            }

            if (e.badge) {
                pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
            }
        }

        // handle GCM notifications for Android
        function onNotificationGCM(e) {
            $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');

            switch( e.event )
            {
                case 'registered':
                if ( e.regid.length > 0 )
                {
                    $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
                    // Your GCM push server needs to know the regID before it can push to this device
                    // here is where you might want to send it the regID for later use.
                    console.log("regID = " + e.regid);
                }
                break;

                case 'message':
                    // if this flag is set, this notification happened while we were in the foreground.
                    // you might want to play a sound to get the user's attention, throw up a dialog, etc.
                    if (e.foreground)
                    {
                        $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');

                        // if the notification contains a soundname, play it.
                        var my_media = new Media("/android_asset/www/"+e.soundname);
                        my_media.play();
                    }
                    else
                    {   // otherwise we were launched because the user touched a notification in the notification tray.
                        if (e.coldstart)
                            $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
                        else
                        $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
                    }

                    $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
                    $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
                break;

                case 'error':
                    $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
                break;

                default:
                    $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
                break;
            }
        }

        function tokenHandler (result) {
            $("#app-status-ul").append('<li>token: '+ result +'</li>');
            // Your iOS push server needs to know the token before it can push to this device
            // here is where you might want to send it the token for later use.
        }

        function successHandler (result) {
            $("#app-status-ul").append('<li>success:'+ result +'</li>');
        }

        function errorHandler (error) {
            $("#app-status-ul").append('<li>error:'+ error +'</li>');
        }

        document.addEventListener('deviceready', onDeviceReady, true);

     </script>

在 GCMIntentService.java onMessage() 函数中添加以下代码行:

String message = extras.getString("message");
String title = extras.getString("title");
Notification notif = new Notification(android.R.drawable.btn_star_big_on, message, System.currentTimeMillis() );
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;

Intent notificationIntent = new Intent(context, TestSampleApp.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

notif.setLatestEventInfo(context, title, message, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
mNotificationManager.notify(1, notif)

并导入这些包

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

推送通知未显示 的相关文章

  • Phonegap 之类的 Mac App Store 平台? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在使用phonegap 在iOS 本机应用程序中运行我的HTML JS 代码 有没有办法在 Mac
  • 如何在android中使用jquery和phonegap打开pdf文件?

    最近我正在为 Android 构建一个应用程序 我正在使用phonegap 来做同样的事情 一切都运行良好 除了一个问题 即我无法在 android 中使用 jquery 打开 pdf 文件 我已经尝试了很多做同样的事情 但我无法做到这一点
  • Phonegap从Java代码获取本地存储值?

    我已经使用phonegap在客户端保存了数据本地存储 http docs phonegap com en 1 2 0 phonegap storage storage md html现在我想用java代码访问保存的数据 这可能吗 我怎样才能
  • 原生编程对于移动开发有何优势?

    我需要为一家公司在一些主要的移动操作系统上开发应用程序 特别是 iOS Android 和 WP7 我最初计划为三种不同的操作系统编写三个独立的应用程序 每个应用程序都使用本机 SDK 然而 这样做有什么好处吗 有许多可用的跨平台工具 Se
  • Cordova android 构建错误:执行“ant clean -f ...”时出错

    当我尝试在 Android 设备上构建并运行 cordova 项目时 出现此错误 Running app on platform android via command Documents App platforms android cor
  • 混合应用程序开发(PhoneGap、Cordova、Ionic)

    我试图了解一切是如何运作的 但对此主题有一些疑问 我将解释我如何理解这些东西 首先让我们从Cordova这是翻译平台JS CSS HTML文件到本机应用程序中 但这并不完全是事实 实际上 它只是将所有 html css 文件放入 asset
  • 使用cordova打开本地pdf文件

    我使用 cordova 3 3 0 我想打开本地 pdf 文件 然后根据用户需求关闭它 ref window open encodeURI path blank location yes ref addEventListener exit
  • Cordova Android 应用程序中的网页不可用

    编辑 我一直在解决这个问题并回顾我的所有步骤 我很乐意缩小这个问题的规模 并在令人困惑的情况下获得更多确切的细节 目前 我觉得 Keycloak 似乎只想将我重定向到 https 据我所知 这应该是 Wildfly 服务器配置问题 编辑 我
  • 没有提示为 Cordova Windows 应用程序签署证书

    我正在尝试使用命令行工具创建和安装 Cordova Windows 应用程序 跑步时cordova run windows device shell 显示此文本 Before installing this app you need to
  • GapDebug:哪里可以获得最新版本?

    Genuitec 宣布 GapDebug 生命周期结束 因此他们不再托管任何安装文件 我想下载最新版本 2 4 0 但找不到托管服务商 None
  • Phonegap - cordova 在 Android 和 iOS 设备上延迟且缓慢

    我刚刚开始使用 zend studio 开始我的第一个 PhoneGap 项目 但是 在我构建并部署它之后 该应用程序非常慢 Android 和 iOS 均可 滚动滞后 如果我按下按钮 转到下一页的速度很慢 有什么办法可以提高它的性能吗 提
  • Cordova 上的 ClearCookiesAsync()

    我正在尝试使用 wp8 cordova 中的插件来清除 WebBrowser cookie 我已经让它与 JavaScript 进行通信 并且我的 c 文件中有类似这样的内容 using WPCordovaClassLib Cordova
  • 如何在 Windows Phone 8 中安装 .xap PhoneGap 版本?

    我使用phonegap android开发了一款简单的游戏 但在为Windows iOS Android 黑莓等构建后 它仅适用于Android设备 因此我可以为Android设备安装apk文件 但在Windows xap文件中未检测到Wi
  • Android 应用程序 Phonegap 中的 Admob 实现

    我将一个 jquery 脚本转换为 Android 应用程序 现在我想将 admob 添加到其中 这可能吗 谷歌从那时起禁止在移动应用程序中使用 AdSense 在移动应用程序中使用 AdSense 移动广告违反了 AdSense 政策 移
  • FileReader 在 Ionic 2 中未触发 onloadend

    我正在尝试使用 cordova file plugin 读取本地文件 目前我可以读取本地目录的内容并选择单个文件 但我在获取文件内容时遇到问题 这是我的函数 从列表中选择文件后单击按钮即可调用该函数 import window resolv
  • 可拖动的非模态弹出窗口 Jquery Mobile

    我希望在 Jquery mobile 中有一个弹出窗口 它不会阻止用户与页面交互 并且 data dismissible false 即当页面的另一部分与页面的另一部分交互并保持可见时 弹出窗口不会消失 我已经尝试过这个 popupNew
  • Cordova Phonegap“导出失败”错误代码 70 构建 ios 时

    我目前正在使用 Cordova Phonegap 构建 iOS 应用程序 本来工作正常 但现在运行时出现错误cordova build ios在终端中 我收到以下错误 导出失败 错误 命令的错误代码 70 带有参数的 xcodebuild
  • ShareKit + Facebook 身份验证不起作用

    我正在使用 ShareKit 从我正在开发的 PhoneGap 应用程序发布到 Facebook 我在 Facebook 中创建了该应用程序 并在我的项目中安装了该插件 Twitter 工作正常 但当我尝试在 Facebook 上分享时出现
  • 在 HTML5 iOS 7 / iOS 8 中显示十进制键盘

    经过几个小时的搜索后 我只是有一个简单的问题 是否有可能在网络浏览器输入字段中显示小数键盘 input type number 只显示数字 但我需要在左下角使用逗号或点 我尝试过任何事情 pattern step等等 但没有显示十进制键盘
  • Phonegap使用命令行工具添加插件

    我是phonegap的新手 我按照phonegap官方网站中定义的步骤使用命令行工具创建项目 nodejs 我成功创建项目并添加平台 但是 当我尝试添加插件时出现以下错误 命令 cordova plugin add https git wi

随机推荐

  • 带元组键的不区分大小写的字典

    我有一本字典 其中键是一个元组 其中第一项是日期 第二项是字符串 我希望字典不区分大小写 我知道 如果键只是一个字符串 我可以在声明字典时将 StringComparer OrdinalIgnoreCase 作为参数传递 但是当键是元组时
  • 如何将 Django QuerySet 转换为字典列表?

    如何转换 DjangoQuerySet into a list of dict是 我还没有找到这个问题的答案 所以我想知道我是否缺少某种每个人都使用的常见辅助函数 Use the values https docs djangoprojec
  • Rails 4:使用 Cocoon Gem 将 child_index 添加到动态添加(嵌套)表单字段

    更新 我正在尝试向涉及多个模型的嵌套表单添加 删除表单字段 我看过 Ryan Bates 的 Dynamic Forms railscast 并且我提到过本文 https hackhands com building has many mo
  • php 我如何从文本文件中获取某些关键字?

    所以在服用了几次 Advil 之后 我想我需要帮助 我正在尝试制作一个脚本 让用户上传 txt 文件 该文件将如下所示 EXT DUNKIN DONUTS DAY Police vehicles remain in the parking
  • 如何在 VB.NET 中声明内联数组

    我正在寻找 VB NET 的等效项 var strings new string abc def ghi Dim strings As String abc def ghi
  • 多种环境中的.htaccess

    我知道以前曾有人问过类似的问题 但我还没有找到适合我的情况的任何真正具体的答案 我有一个在多个环境 本地 开发 生产 上运行的 ExpressionEngine 站点 每个环境都需要不同的 htaccess 规则 所有环境 删除index
  • 如何将列表从控制器传递到asp.net mvc中的javascript函数?

    我在控制器中有这个查询 DataClasses1DataContext behzad new DataClasses1DataContext var query from p in behzad ImagePaths select new
  • Jekyll 无法服务(Ruby 不兼容的库版本)

    当尝试跑步时jekyll serve 它似乎构建正确 尽管有我无法解决的警告 但随后无法提供服务 jekyll build完成 但生成的文件缺少已编译的 CSS 输出来自jekyll serve Ignoring ffi 1 9 10 be
  • PetaPoco 处理枚举吗?

    我正在尝试使用 PetaPoco 将表转换为 POCO 在我的表中 有一列名为TheEnum 此列中的值是表示以下枚举的字符串 public enum MyEnum Fred Wilma 当 PetaPoco 试图将字符串 Fred 转换为
  • 如何快速打印数组中对象的值,而不是其位置

    我有一个类 其中包含同一项目中单独的 swift 文件中某些企业家的数据 它看起来像这样 class Entrepreneur NSObject var name String var netWorth 0 0 var company St
  • 如何在不改变编码风格的情况下避免空白锚下划线?

    看看下面的小提琴 http jsfiddle net DNhAk 14 http jsfiddle net DNhAk 14 当您的图像的文本包含在锚点 链接中时 代码中图像和文本之间的空白会在呈现的页面中的文本之前创建一个带下划线的空白
  • RIFF/Wav 标头中的“LIST”块是什么?

    我正在编写一个 wav 播放器 并且使用以下文件格式规范 http soundfile sapp org doc WaveFormat http soundfile sapp org doc WaveFormat 正如您所看到的 它期望 一
  • 返回类型的具体类型或接口?

    今天我遇到了对象编程风格 具体类型或接口的一个基本悖论 对于方法的返回类型 哪个更好 具体类型还是接口 在大多数情况下 我倾向于使用具体类型作为方法的返回类型 因为我相信具体类型对于进一步使用更加灵活并且公开更多功能 其阴暗面 耦合 天使般
  • 使用窗口 ID 激活窗口

    我将如何以编程方式激活 即移动到前面并聚焦 macOS 上的窗口 不属于我的应用程序 Window ID 我的应用程序将在用户授予辅助权限等的情况下运行 令人惊讶的是 上面没有描述任何功能石英窗服务页面 https developer ap
  • 如何在每个页面上多次使用具有相同类名的 Masonry?

    我需要每页有多个砌体网格 我使用 wordpress 循环生成代码 因此每个 div 容器都具有相同的类名 有没有办法在所有同名的 div 容器上调用 Masonry html div class print slider div clas
  • PHP:三元运算符

    这似乎是一个非常简单的问题 但我尝试的一切都给了我一个错误 if query results gt have posts count results query results gt found posts 问题 如何添加else这段代码的
  • Erlang停止gen_server

    我有 gen server start UserName gt case gen server start global UserName player of ok gt io format Player UserName started
  • 为什么我的 img 错误函数失败?

    我动态构建的一些 img 元素可能会失败 对于这些情况 我从这里得到了一些代码 有没有办法以编程方式确定图像链接是错误的 https stackoverflow com questions 17702123 is there a way t
  • PHP:类似 Youtube 的短 ID,带有盐

    我需要对数据库 ID 进行编码 加密并将其附加到我的 URL 中 安全性不是我想要解决的问题 但我正在寻找具有中等安全性的东西 主要目标是拥有唯一且 URL 安全的短 ID 下面的代码片段似乎可以满足我的需要 来自http programa
  • 推送通知未显示

    我开发了一个使用推送插件的phonegap应用程序 我创建了一个示例服务器 推送消息未显示在通知栏中 但消息正在应用程序中显示 请帮忙 我的代码是 var 推送通知 function onDeviceReady app status ul