GCM通知的自定义用户界面 [英] Custom UI for a GCM Notification

查看:81
本文介绍了GCM通知的自定义用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCM文档中给出:


它不为
的消息数据提供任何内置用户界面或其他处理。 GCM只是将收到的原始消息数据直接传递给Android应用程序的
,Android应用程序完全控制着它的处理方式。
例如,应用程序可能会发布通知,显示
自定义用户界面或静默同步数据

但没有关于如何创建自定义通知用户界面的信息。



如何创建自定义用户界面,比如说带有2个按钮等的小对话框,用于GCM通知。就像gmail提供了一个选项来存档或从状态栏通知中删除邮件。



CODE:

  public void onReceive(Context context,Intent intent){
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
字符串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通知)
。 setContentText(MSG);

mBuilder.setContentIntent(contentIntent);
通知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);

谢谢



因为这与GCM无关。


如何创建自定义用户界面,比如说带有2个按钮的小对话框等。 ,用于GCM通知。就像gmail提供了一个选项来存档或从状态栏通知中删除邮件。


这是一个扩展或大如文档中所述。


In GCM Docs its given:

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

But nothing about how to create a custom notification UI.

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.

CODE:

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);
}

Thank You

解决方案

But nothing about how to create a custom notification UI.

Because that has nothing to do with 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.

That is an expanded or "big" notification, as is covered in the documentation.

这篇关于GCM通知的自定义用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆