簡體   English   中英

Android Notification使用PendingIntent不是新的

[英]Android Notification use PendingIntent not new one

我已經在我的應用程序中實現了一些通知。 似乎每次我單擊通知都會啟動新的意圖。 目標是將應用程序置於前台,因為它仍在后台(因此不會被殺死)。

我該如何實現?

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

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, ChatActivity.class), 0);
    long[] vibration = new long[]{100, 250}; 

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.icon)
    .setAutoCancel(true)
    .setTicker(msg)
    .setLights(Color.WHITE, 300, 600)
    .setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.new_msg))
    .setVibrate(vibration)
    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

要將應用程序置於最前面,請在“待定意圖”中使用以下意圖。

Intent intent = new Intent(this, ChatActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

如果尚未終止應用程序,它將恢復該應用程序;如果已終止它,則將打開ChatActivity。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM