向通知列表/栏发布推送通知时,.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'
最佳答案
有可能你的contentText在支持大文本样式的设备上没有显示,那是因为你没有设置大文本就将样式设置为BigTextStyle。
代替
.setStyle(new NotificationCompat.BigTextStyle())
你应该试试
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
这解释了为什么在某些设备上您没有遇到问题:
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.
这解释了为什么在某些设备上您会这样做:
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.
引自 here .
关于android - NotificationManager.notify 创建*最初*缺少 contentText 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17735027/