在GCM文档中给出:
它不提供任何内置用户界面或其他处理消息数据。GCM简单地将接收到的原始消息数据直接传递给Android应用程序,它完全控制如何处理它。例如,应用程序可能发布通知,显示自定义用户界面,或静默同步数据
但是没有提到如何创建自定义通知UI。
如何创建一个自定义UI,比如一个带有2个按钮的小对话框等。,以获取GCM通知。就像gmail提供了一个从状态栏通知中存档或删除邮件的选项。
代码:
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
} else {
sendNotification(intent.getExtras().getString("msg"));
}
setResultCode(Activity.RESULT_OK);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, NotificationsActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
Notification mNotification = mBuilder.getNotification();
SharedPreferences sp = ctx.getSharedPreferences(
GCMDemoActivity.GCM_NOTIF_PREF, Context.MODE_PRIVATE);
long diff = System.currentTimeMillis()
- sp.getLong("last_gcm_timestamp", 0);
if (diff > TWO_MINUTES) {
mNotification.defaults = Notification.DEFAULT_ALL;
SharedPreferences.Editor editor = sp.edit();
editor.putLong("last_gcm_timestamp", System.currentTimeMillis());
editor.commit();
}
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
谢谢
但是没有提到如何创建自定义通知UI。
因为这与GCM无关。
如何创建一个自定义UI,比如一个带有2个按钮的小对话框等。,以获取GCM通知。就像gmail提供了一个从状态栏通知中存档或删除邮件的选项。
这是一个扩展或"大"通知,如文档中所述。
当GCMIntentService.java类中的generateNotification()从GCM接收通知时,可以创建一个带有两个按钮的对话框