NotificationManager.notify创建“最初"缺少contentText的通知 [英] NotificationManager.notify creating notifications that are *initially* missing contentText
问题描述
在向通知列表/栏发布推送通知时,最初不会显示.contentText和.number(.ticker,.icon和.contentTitle显示正常).但是,在发布另一个通知(具有不同的ID)之后,当第一个通知在列表中被击倒时,它将显示内容文本和编号.然后新的缺少文本,依此类推.
由于我使用毫秒计时器来创建唯一的ID,因此我认为以某种方式更新上一篇文章是不可能的.因此,我必须首先在发布时出现错误,以某种方式导致它丢失文本,直到不再是最新文本为止.
该问题仅在某些设备上发生-主要在Nexus Tablets(运行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而未设置大文本./p>
代替
.setStyle(new NotificationCompat.BigTextStyle())
您应该尝试
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
这说明了为什么在某些设备上没有遇到问题的原因:
NotificationCompat.BigTextStyle
Helper类,用于生成包含大量文本的大幅面通知. 如果平台不提供大幅面通知,则此方法无效.用户将始终看到普通的通知视图.
这说明了为什么在某些设备上这样做:
public NotificationCompat.BigTextStyle bigText(CharSequence cs)
以较长的模板形式提供更长的文本,以模板代替内容文本.
引用来自此处.
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.
这篇关于NotificationManager.notify创建“最初"缺少contentText的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!