use*_*779 4 android google-cloud-messaging
在GCM Docs中给出了:
它不为消息数据提供任何内置用户界面或其他处理.GCM只是将收到的原始消息数据直接传递给Android应用程序,Android应用程序完全控制如何处理它.例如,应用程序可能会发布通知,显示自定义用户界面或静默同步数据
但没有关于如何创建自定义通知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);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
| 归档时间: |
|
| 查看次数: |
3223 次 |
| 最近记录: |