簡體   English   中英

單擊通知時啟動活動

[英]Launch Activity when notification is clicked

當我從狀態欄中單擊通知時,我想打開一個活動。 我已經在StackOverflow上看到了這個答案,但是這些答案都不適合我。 這是我的代碼:

    notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setProgress(100, 0, false);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload);
    notificationBuilder.setContentTitle(getString(R.string.notification_upload_title));

    //when this notification is clicked and the upload is running, open the upload fragment
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    // set intent so it does not start a new activity
    PendingIntent intent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    notificationBuilder.setContentIntent(intent);

    this.startForeground(NOTIFICATION_ID_UPLOADING_PROGRESS, notificationBuilder.build());

的Manifest.xml

<activity
        android:name="com.android.app.MainActivity_"
        android:label="@string/app_short_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

終於我意識到我必須寫這個:

Intent notificationIntent = new Intent(this, MainActivity_.class);

代替

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

非常感謝您的所有回答!

我認為您不需要所有這些標志和配置就可以從PendingIntent中打開活動。 你當然不需要

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent.FLAG_ONE_SHOT

刪除它們,然后重試。

此外:MainActivity的包名稱是否符合預期( com.android.app.MainActivity )?

請嘗試以下解決方案,希望它對您有用

Intent intent = new Intent(this,YourActivity....class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent blogIntent = PendingIntent.getActivity(this, INT CONSTANTS, intent,
                PendingIntent.FLAG_ONE_SHOT);

從通知

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
.
.
.
notificationBuilder.setContentIntent(blogIntent);


Notification notification = notificationBuilder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(Constants.NOTIFICATION_NEW_BLOG, notification);

您必須在清單中添加自定義接收方

       <receiver
            android:name=".IntentReceiver"
            android:exported="false">
            <intent-filter>

                <!-- add notification action here  -->

            </intent-filter>
        </receiver>

在Intent接收器類中,在recipe方法上重寫

public class IntentReceiver extends BroadcastReceiver{
@Override
    public void onReceive(Context context, Intent intent) {
        //call your activity here
    }
} 
Use this code

/* Invoking the default notification service */
        NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);

        mBuilder.setContentTitle("Test");
        mBuilder.setContentText(text2);
        mBuilder.setSmallIcon(R.drawable.icon);

   /* Creates an explicit intent for an Activity in your app */
        Intent resultIntent = new Intent(this, NotificationListActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(NotificationListActivity.class);

   /* Adds the Intent that starts the Activity to the top of the stack */
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

   /* notificationID allows you to update the notification later on. */
        mNotificationManager.notify(9999, mBuilder.build());

暫無
暫無

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

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