NotificationManager.notify creating notifications that are *initially* missing contentText

拈花ヽ惹草 提交于 2020-06-28 07:25:10

问题


When posting a push notification to the Notifications List/Bar, the .contentText and the .number are initially not displayed (.ticker, .icon and .contentTitle display fine). However, after posting another notification (with a different ID), when the first one gets bumped down in the list, it then displays the content text and number. And then the new one is missing the text, and so on.

Since I'm using the millisecond timer to create a unique ID, I don't think it's possible for me to be somehow updating the previous post. So I must be posting it initially with something wrong such that somehow causes it to be missing the text until it's no longer the most recent one.

The problem only happens on some devices -- mostly on nexus tablets (running 4.2.2). On mosts phones seems to work fine. On any given device it either always works or never works. It's not intermittent in that sense.

Here's the code that responds to the push and posts to the notification center.

public class GcmBroadcastReceiver extends BroadcastReceiver {
   static final String TAG = "GmcBroadcastReceiver";
   Context ctx;

   @Override
   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)) 
           Log.i(TAG, "PUSH RECEIVED WITH ERROR: " + intent.getExtras().toString());
       else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) 
           Log.i(TAG, "DELETED PUSH MESSAGE: " + intent.getExtras().toString());
       else
       {
           Log.i(TAG, "Received PUSH: " + intent.getExtras().toString());
           if (MyApp.isAppForeground == false)
              postNotification(intent.getExtras());
       }
       setResultCode(Activity.RESULT_OK);
   }

  // post GCM message to notification center.
  private void postNotification(Bundle data) {
     String msg = data.getString("alert");
     Log.i(TAG, "message: " + msg);

     if (msg == null)  // on app startup, this was always getting called with empty message
        return;

     int badge = Integer.parseInt(data.getString("badge","0"));

     Intent intent = new Intent(ctx, WordChums.class);
     PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, 0); //, data);

     Uri sound = Uri.parse("android.resource://com.peoplefun.wordchums/raw/push");
     NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
     .setSmallIcon(R.drawable.ic_stat_gcm)
     .setContentTitle("Word Chums")
     .setContentText(msg)
     .setTicker(msg)
     .setStyle(new NotificationCompat.BigTextStyle())
     .setAutoCancel(true)
     .setOnlyAlertOnce(true)
     .setSound(sound)
     .setDefaults(Notification.DEFAULT_VIBRATE); 
     if (badge > 0)
        builder.setNumber(badge);

     builder.setContentIntent(contentIntent);
     NotificationManager notificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
     notificationManager.notify((int)System.currentTimeMillis(), builder.build());
  }
}

The log entries that print are as expected.

I/GmcBroadcastReceiver( 2081): Received PUSH: Bundle[{gm=37206155, collapse_key=do_not_collapse, alert=TestUser said: 'Message 1', sound=push, badge=6, from=550952899880, pfok=1, ct=1}]
I/GmcBroadcastReceiver( 2081): message: TestUser said: 'Message 1'
I/GmcBroadcastReceiver( 2081): Received PUSH: Bundle[{gm=37206155, collapse_key=do_not_collapse, alert=TestUser said: 'Message 2', sound=push, badge=6, from=550952899880, pfok=1, ct=1}]
I/GmcBroadcastReceiver( 2081): message: TestUser said: 'Message 2'
I/GmcBroadcastReceiver( 2081): Received PUSH: Bundle[{gm=37206155, collapse_key=do_not_collapse, alert=TestUser said: 'Message 3', sound=push, badge=6, from=550952899880, pfok=1, ct=1}]
I/GmcBroadcastReceiver( 2081): message: TestUser said: 'Message 3'

回答1:


It's possible that your contentText is not displayed on devices that support big text style, and that's because you set the style to be BigTextStyle without setting the big text.

Instead of

.setStyle(new NotificationCompat.BigTextStyle())

You should try

.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))

This explains why on some devices you don't encounter the problem:

NotificationCompat.BigTextStyle

Helper class for generating large-format notifications that include a lot of text. If the platform does not provide large-format notifications, this method has no effect. The user will always see the normal notification view.

And this explains why on some devices you do:

public NotificationCompat.BigTextStyle bigText (CharSequence cs)

Provide the longer text to be displayed in the big form of the template in place of the content text.

Quotes taken from here.




回答2:


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Event tracker")
.setContentText("Events received")

NotificationCompat.InboxStyle inboxStyle =
    new NotificationCompat.InboxStyle();

String[] events = new String[6];
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the big view
for (int i=0; i < events.length; i++) {

    inboxStyle.addLine(events[i]);
}
// Moves the big view style object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.

See Android Notification




回答3:


Check for the msg field by keeping syso or in debugging I think it might be an empty string you are getting

(or)

Try this code surely works for you and also to display multiple notifications

NotificationCompat.Builder nBuilder;
    Uri alarmSound = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    nBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Kluebook - " + keys)
            .setLights(Color.BLUE, 500, 500).setContentText(message)
            .setAutoCancel(true).setTicker("Notification from kluebook")
            .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
            .setSound(alarmSound);
    Intent resultIntent = null;
    resultIntent = new Intent(context, MainLoginSignUpActivity.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    if (notify_no < 9) {
        notify_no = notify_no + 1;
    } else {
        notify_no = 0;
    }
    nBuilder.setContentIntent(resultPendingIntent);
    NotificationManager nNotifyMgr = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);
    nNotifyMgr.notify(notify_no + 2, nBuilder.build());
}


来源:https://stackoverflow.com/questions/17735027/notificationmanager-notify-creating-notifications-that-are-initially-missing-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!