在GCM Docs中给出:
它不提供任何内置用户界面或对消息数据的其他处理。GCM 只是将接收到的原始消息数据直接传递给 Android 应用程序,它可以完全控制如何处理它。例如,应用程序可能会发布通知、显示自定义用户界面或静默同步数据
但没有关于如何创建自定义通知 UI。
如何为 GCM 通知创建自定义 UI,例如带有 2 个按钮的小对话框等。就像 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);
}
谢谢你