android - GCM 通知的自定义 UI

标签 android google-cloud-messaging

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/

相关文章:

android - 如何使用 Android 的 java 在 LinearLayout 上设置 marginTop?

java - 无法获取提供商 com.google.android.gms.measurement.AppMeasurementContentProvider

android - 向所有注册设备广播通知

ios - GCM IOS 错误 - 注册到 GCM 失败,错误为 : The operation couldn’t be completed.(com.google.iid 错误 7。)

c# - 该请求缺少身份验证 key (FCM token )

android - Google Cloud Messaging 注册方法因 IOException (MAIN_THREAD) 而失败

android - 将对象传递给 Intent 不是直截了当的原因吗?

android - eclipse 不在 logcat 中仅显示 galaxy nexus 的时间?

安卓奇巧 : Snackbar is not in the bottom of the screen

android - GCM 的注册 ID 重复