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'
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.