Android NotificationManager.notify正在创建*最初*缺少内容文本的通知
将推送通知发布到通知列表/栏时,最初不显示.contentText和.number(.ticker、.icon和.contentTitle显示良好)。但是,在发布另一个通知(使用不同的ID)后,当第一个通知在列表中被删除时,它会显示内容文本和编号。然后新的一个丢失了文本,等等 由于我使用毫秒计时器来创建一个唯一的ID,我认为我不可能以某种方式更新上一篇文章。因此,我必须先发布一些错误的内容,以某种方式导致它丢失文本,直到它不再是最新的 这个问题只发生在一些设备上——大部分是nexus平板电脑(运行4.2.2)。在大多数手机上似乎都能正常工作。在任何给定的设备上,它要么总是工作,要么从不工作。从这个意义上讲,它不是间歇性的 下面是响应推送并发送到通知中心的代码Android NotificationManager.notify正在创建*最初*缺少内容文本的通知,android,push-notification,google-cloud-messaging,android-notifications,android-notification-bar,Android,Push Notification,Google Cloud Messaging,Android Notifications,Android Notification Bar,将推送通知发布到通知列表/栏时,最初不显示.contentText和.number(.ticker、.icon和.contentTitle显示良好)。但是,在发布另一个通知(使用不同的ID)后,当第一个通知在列表中被删除时,它会显示内容文本和编号。然后新的一个丢失了文本,等等 由于我使用毫秒计时器来创建一个唯一的ID,我认为我不可能以某种方式更新上一篇文章。因此,我必须先发布一些错误的内容,以某种方式导致它丢失文本,直到它不再是最新的 这个问题只发生在一些设备上——大部分是nexus平板电脑(运
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'
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(此)
.setSmallIcon(R.drawable.notification_图标)
.setContentTitle(“事件跟踪器”)
.setContentText(“收到的事件”)
NotificationCompat.InboxStyle InboxStyle=
新的NotificationCompat.InboxStyle();
字符串[]事件=新字符串[6];
//设置收件箱样式大视图的标题
inboxStyle.setBigContentTitle(“事件跟踪器详细信息:”);
...
//将事件移动到大视图中
for(int i=0;i
请参见NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(此)
.setSmallIcon(R.drawable.notification_图标)
.setContentTitle(“事件跟踪器”)
.setContentText(“收到的事件”)
NotificationCompat.InboxStyle InboxStyle=
新的NotificationCompat.InboxStyle();
字符串[]事件=新字符串[6];
//设置收件箱样式大视图的标题
inboxStyle.setBigContentTitle(“事件跟踪器详细信息:”);
...
//将事件移动到大视图中
for(int i=0;i
请参阅通过保留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());
}
NotificationCompat.Builder-nBuilder;
Uri alarmSound=铃声管理器
.getDefaultUri(RingtoneManager.TYPE_通知);
nBuilder=新通知compat.Builder(上下文)
.setSmallIcon(R.drawable.ic_启动器)
.setContentTitle(“Kluebook-”+键)
.setLights(Color.BLUE,500500).setContentText(消息)
.setAutoCancel(true).setTicker(“来自kluebook的通知”)
.setVibrate(新长[]{100250100250100250})
.设置声音(报警声音);
Intent resultent=null;
resultIntent=新意图(上下文,MainLoginSignUpActivity.class);
PendingEvent ResultPendingEvent=PendingEvent.getActivity(上下文,
通知(否、结果、挂起内容、标志更新(当前));
如果(通知编号<9){
notify_no=notify_no+1;
}否则{
通知_no=0;
}
nBuilder.setContentIntent(resultPendingContent);
NotificationManager nNotificationMgr=(NotificationManager)上下文
.getSystemService(上下文通知服务);
notifymgr.notify(notify_no+2,nBuilder.build());
}
通过保留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());
}
NotificationCompat.Builder-nBuilder;
Uri alarmSound=铃声管理器
.getDefaultUri(RingtoneManager.TYPE_通知);
nBuilder=新通知compat.Builder(上下文)
.setSmallIcon(R.drawable.ic_启动器)
.setContentTitle(“Kluebook-”+键)
.setLights(Color.BLUE,500500).setContentText(消息)
.setAutoCancel(true).setTicker(“来自kluebook的通知”)
.setVibrate(新长[]{100250100250100250})
.设置声音(报警声音);
Intent resultent=null;
resultIntent=新意图(上下文,MainLoginSignUpActivity.class);
PendingEvent ResultPendingEvent=PendingEvent.getActivity(上下文,
通知(否、结果、挂起内容、标志更新(当前));
如果(通知编号<9){
notify_no=notify_no+1;
}否则{
通知_no=0;
}
nBuilder.setContentIntent(resultPendingContent);
NotificationManager nNotificationMgr=(NotificationManager)上下文
.getSystemService(上下文通知服务);
notifymgr.notify(notify_no+2,nBuilder.build());
}
您的contentText可能不显示在支持大文本样式的设备上,这是因为您在未设置大文本的情况下将样式设置为BigTextStyle
而不是
.setStyle(新的NotificationCompat.BigTextStyle())
你应该试试
.setStyle(新通知compat.BigTextStyle().bigText(msg))
这解释了为什么在某些设备上没有遇到问题:
NotificationCompat.BigTextStyle
用于生成包含大量文本的大格式通知的帮助器类。
如果平台不提供大格式通知,则此方法无效。用户将始终看到正常的通知视图
这就解释了为什么在s