今天项目中用到推送,集成了个推。在其透传消息的方法中调用下面的方法就可以发送通知到通知栏,点击之后启动应用或者跳到需要的界面
private void showNotification(Context context, String message)
{
Notification notifi = new Notification(R.drawable.notification, message,
System.currentTimeMillis());
notifi.defaults |= Notification.DEFAULT_SOUND; //开启了声音
notifi.defaults |= Notification.DEFAULT_VIBRATE; //开启了震动
notifi.flags = Notification.FLAG_AUTO_CANCEL; //设置点击之后消失
Intent i;
i = new Intent(context, MainActivity.class);
// i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.putExtra("jump", 1100);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
notifi.setLatestEventInfo(context, "title", message, pIntent);
NotificationManager nMgr = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int notifyId = (int)(system.currenttimemillis()/10000)
nMgr.notify(notifyId notifi); //notifyId 如果相同的话 通知栏只会显示最后一个通知,如果不同的话 通知栏会一条一条往下排列出来
}