在Notification類中setLatestEventInfo()方法顯示過期,所以使用另外一種方式建立一個簡單的通知(PS:在Activity中)java
NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder=new Builder(context); builder.setContentTitle("xxxx");//設置標題 builder.setContentText("xxxx");//設置內容 builder.setSmallIcon(com.example.pushdemo2.R.drawable.ic_launcher);//定義左邊的小圖標 builder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE);//設置提示聲音 Intent intent=new Intent(context, Main.class); PendingIntent penIntent=PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, null); builder.setContentIntent(penIntent);//設置跳轉界面 builder.setAutoCancel(true);//設置點擊通知欄後自動消失 Notification notification=builder.build(); notificationManager.notify(1, notification);