C2DM показывает уведомления в области уведомлений, таких как gmail - PullRequest
2 голосов
/ 28 ноября 2011

Я экспериментировал с c2dm, и он работает, и я получаю сообщение (я печатаю простой тост).Как настроить уведомление таким же образом, как уведомление для Gmail, например, чтобы показывать меня в области уведомлений, в верхнем правом углу, когда я получаю новое?Есть ли флаг для этого или API?(В момент я получаю сообщение в коде, извлекаю из намерения по ключу и показываю тост, но в области уведомлений ничего нет).

1 Ответ

4 голосов
/ 28 ноября 2011

В вашем классе C2DMReceiver, который расширен от C2DMBaseReceiver.Поместите следующий код в функцию переопределения onMessage, а также напишите функцию с именем createNotification (), которая приведена ниже.

@Override
protected void onMessage(Context context, Intent intent) {      
    Bundle extras = intent.getExtras();
    if (extras != null) {
        String msg = extras.getString("data.c2dmsg");
        String msgTitle = extras.getString("data.c2dmsgtitle");
        String msgTicker = extras.getString("data.c2dmsgticker");
        createNotification(msgTitle, msg, msgTicker);
    }
}


 public void createNotification(String title, String messageText, String tickerttext) {
      int icon = R.drawable.ic_stat_notify_msg; // icon from resources
      CharSequence tickerText = tickerttext; // ticker-text
      long when = System.currentTimeMillis(); // notification time
      Context context = getApplicationContext(); // application Context
      CharSequence contentTitle = title; // expanded message title
      CharSequence contentText = messageText; // expanded message text
      Intent notificationIntent = new Intent(this, HomekhawarActivity.class);

      Bundle xtra = new Bundle();
      xtra.putString("title", title);
      xtra.putString("message", messageText);

      notificationIntent.putExtras(xtra);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        notificationIntent, PendingIntent.FLAG_ONE_SHOT
          + PendingIntent.FLAG_UPDATE_CURRENT);
      String ns = Context.NOTIFICATION_SERVICE;

      NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
      Notification notification = new Notification(icon, tickerText, when);
      notification.setLatestEventInfo(context, contentTitle, contentText,   contentIntent);
      notification.defaults |= Notification.DEFAULT_LIGHTS;
      notification.defaults |= Notification.DEFAULT_SOUND;
      notification.defaults |= Notification.FLAG_AUTO_CANCEL;
      notification.flags = Notification.DEFAULT_LIGHTS
        | Notification.FLAG_AUTO_CANCEL;
      final int HELLO_ID = rand.nextInt();
      mNotificationManager.notify(HELLO_ID, notification);
    }
...