我想使用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/