4

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

user1537779
  • 2,311
  • 5
  • 28
  • 45
  • What is in your `GCMIntentService` ? – Raptor May 31 '13 at 10:28
  • I am not using any IntentService, I am building the Notification in the `BroadcastReceiver's onReceive()` – user1537779 May 31 '13 at 10:30
  • oops, my bad. i should ask the content of `onReceive()` function. – Raptor May 31 '13 at 10:33
  • What you can do is, create a DialogActivity having custom layout and when you receive any new message, create that activity and show it. This will show message popup with dialog. – Chintan Rathod May 31 '13 at 10:38
  • @ChintanRathod that sounds like a advertisement dialog which asks for install some apps if am right. But that doesn't look like a notification. Cant I do something similar in the status bar? – user1537779 May 31 '13 at 10:41
  • you can.. why not.. I think you can create a view which handles all the notification like we show in top of websites, those were scrolling with marquee effect. You can display like that. You need to create a base activity for that which have base layout with bottom panel. – Chintan Rathod May 31 '13 at 10:43

2 Answers2

5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • fine. Another thing is the text doesn't come on the top status bar. Always just the icon comes. I have seen apps whose notification message comes on top for like 1-2 seconds and the icon remains. – user1537779 May 31 '13 at 11:40
  • @user1537779: That is the ticker text, which you set via `setTicker()`. – CommonsWare May 31 '13 at 11:42
0

You can create a dialog box with two buttons when notification is received from GCM in generateNotification() inside GCMIntentService.java class.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69