1

将推送通知发布到通知列表/栏时,.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'
4

3 回答 3

4

有可能您的 contentText 在支持大文本样式的设备上没有显示,那是因为您将样式设置为 BigTextStyle 而没有设置大文本。

代替

.setStyle(new NotificationCompat.BigTextStyle())

你应该试试

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

这解释了为什么在某些设备上您没有遇到问题:

NotificationCompat.BigTextStyle

用于生成包含大量文本的大型通知的帮助程序类。 如果平台不提供大幅面通知,则此方法无效。用户将始终看到正常的通知视图

这解释了为什么在某些设备上你这样做:

公共 NotificationCompat.BigTextStyle bigText (CharSequence cs)

提供较长的文本以代替内容文本以模板的大格式显示。

行情取自这里

于 2013-08-23T22:24:09.737 回答
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

于 2013-08-23T06:15:46.647 回答
1

通过保持 syso 或在调试中检查 msg 字段我认为它可能是您得到的空字符串

(或者)

试试这个代码肯定对你有用,也可以显示多个通知

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());
}
于 2013-08-23T06:43:47.423 回答