gpt4 book ai didi

android - GCM 通知的自定义 UI

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:07 25 4
gpt4 key购买 nike

GCM Docs它给定的:

It does not provide any built-in user interface or other handling for message data. GCM simply passes raw message data received straight to the Android application, which has full control of how to handle it. For example, the application might post a notification, display a custom user interface, or silently sync data

但没有关于如何创建自定义通知 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);
}

谢谢

最佳答案

But nothing about how to create a custom notification UI.

因为这与 GCM 无关。

How to create a custom UI like say a small dialog with 2 buttons etc.., for a GCM notification. Like gmail gives an option to archive or delete the mail from the status bar notification.

这是一个扩展的或“大”通知,如 the documentation 中所述.

关于android - GCM 通知的自定义 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16855135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com