无奈无奈,昨天写了推送,弹出通知,点击进入消息页面,第一次打开还好,之后数据一直不刷新,总是第一次打开的那些数据,顿时觉得这个世界都不好了。
原代码如下:
/**
* 发通知
*
* @param context
* @param intent
* @param title
* @param content
*/
public void sendNotification(Context context, Intent intent, String title, String content) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText(content);
mBuilder.setWhen(System.currentTimeMillis());
PendingIntent pendingintent = PendingIntent.getActivity(context, 0, intent, 0);
mBuilder.setContentIntent(pendingintent);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_ALL;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify((int) System.currentTimeMillis(), notification);
}
后来发现研究半天才发现了问题
“PendingIntent pendingintent = PendingIntent.getActivity(context, 0, intent, 0);”
这句话的参数问题稍微修改下就好了,如下标红的部分。
PendingIntent pendingintent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
声明:getActivity(Context, int, Intent, int)
,getBroadcast(Context, int, Intent, int)
, and getService(Context, int, Intent, int)
.就是一样的。
俗话说知其然不能不知其所以然,打开官网看一看这个方法
看看这几个参数
Context 上下文对象
requsetCode 这个自己定义,主要就是Activity之间数据传递的,在Activity的onActivityResult(int requestCode, int resultCode, Intent data)里获取返回数据的
intent 打开某个Activity 意图
flags 这个比较关键,它关系到Intent的改变。仔细看看这几个值
FLAG_ONE_SHOT
Flag indicating that this PendingIntent can be used only once. For use with getActivity(Context, int, Intent, int)
, getBroadcast(Context, int, Intent, int)
, and getService(Context, int, Intent, int)
.
If set, after send()
is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
PendingIntent只能使用一次。之后再使用就会失败。
FLAG_NO_CREATE
Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it. For use with getActivity(Context, int, Intent, int)
, getBroadcast(Context, int, Intent, int)
, and getService(Context, int, Intent, int)
.
如果意图不存在则返回null,而不会创建。
FLAG_CANCEL_CURRENT
Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one. For use with getActivity(Context, int, Intent, int)
,getBroadcast(Context, int, Intent, int)
, and getService(Context, int, Intent, int)
.
You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT
.
如果意图之前存在,则取消之前的,重新生成一个新的,可以使用这个新意图中携带的数据。
FLAG_UPDATE_CURRENT
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int)
,getBroadcast(Context, int, Intent, int)
, and getService(Context, int, Intent, int)
.
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
如果意图早就存在,这个意图会保存下来,但是会更替它的数据。如果只是有数据改变可以使用它。
我的英语一般般,翻译的勉强,添加了自己的理解。意会就好。