android - 如何使用phonegap在状态栏中显示android的推送通知?

标签 android cordova push-notification

我想使用phonegap在android中实现推送通知,我使用下面的链接来实现GCM

https://github.com/marknutter/GCM-Cordova

我能够实现推送通知,并且在提交 appid regid 和发件人 ID 后,我能够在移动屏幕上获得一个小示例文本,但我想在状态栏中显示通知。任何人都可以帮助我如何使用上面的示例在状态栏中显示推送通知。

最佳答案

您可以使用Cordova StatusBarNotification plugin 或者,如果您想要更快的解决方案,您可以简单地将以下 native Java 代码添加到 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;

请务必将 YourActivityClassName.class 替换为扩展 DroidGap 的 stub 类的名称。例如,在示例项目中,它称为 MainActivity

关于android - 如何使用phonegap在状态栏中显示android的推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12835058/

相关文章:

android - 适用于 Android 的 Google Play 音乐 API

javascript - TypeError : Cannot read property 'toString' of undefined at Geolib. isDecimal

android - 如何从android中的xtify sdk获取PAYLOAD值?

android - HTML5 Web SQL 数据库问题

javascript - 如何使用 phonegap 在 iOS 应用程序中打开网页?

javascript - 如何启动我的线上和线下事件

ios - 多个设备之间的通信

android - 带有自定义图像的 ionic ionic 选项卡图标

Android APK [INSTALL_FAILED_NO_MATCHING_ABIS : Failed to extract native libraries, res=-113]

android - 如何在调用期间取消 AsyncTask?