将推送通知发布到通知列表/栏时,.contentText 和 .number 最初不显示(.ticker、.icon 和 .contentTitle 显示正常)。但是,在发布另一个通知(使用不同的 ID)后,当第一个通知在列表中被撞倒时,它会显示内容文本和编号。然后新的缺少文本,依此类推。
由于我使用毫秒计时器来创建唯一 ID,因此我认为我不可能以某种方式更新上一篇文章。所以我必须在最初发布它时出现错误,以某种方式导致它丢失文本,直到它不再是最新的。
这个问题只发生在某些设备上——主要是在 nexus 平板电脑上(运行 4.2.2)。在大多数手机上似乎工作正常。在任何给定的设备上,它要么总是工作,要么永远不工作。从这个意义上说,它不是间歇性的。
这是响应推送并发布到通知中心的代码。
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());
}
}
打印的日志条目符合预期。
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'