在GCM文档中给出了以下内容:
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);
}
谢谢
GCMIntentService
里面有什么? - RaptorBroadcastReceiver
的onReceive()
方法中构建通知。 - user1537779onReceive()
函数的内容。 - Raptor