Notification 改新寫法

Notification Noti=new Notification();

Intent i = new Intent(this, MainActivity.class);

   i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);      
   
   
Noti.icon = R.drawable.notification_icon; // notification icon id

Noti.tickerText=mc.getResources().getString(R.string.app_name);

Noti.when=System.currentTimeMillis();
    
Noti.flags=Notification.FLAG_ONGOING_EVENT;
   
// setLatestEventInfo 已被放棄
Noti.setLatestEventInfo(
mc, mc.getResources().getString("My notification"),
mc.getResources().getString("Hello World!") ,
PendingIntent.getActivity(this, 0,i, 0)
);

NotificationManager myNM =
(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);



myNM.notify(0,Noti);

改成如下

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");

Intent i = new Intent(this, MainActivity.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(i);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

留言

這個網誌中的熱門文章

python 找圖自動點擊

Python pyserial 抓取系統內的 COM PORT

VBA EXCEL 工作表變化 馬上執行 的作法 Worksheet_Change