简体   繁体   中英

How to start activity from notification in Android 10?

I implement Firebase push notifications, but in notifications, I want to open a specific Activity, it works fine below android 10, and in Android 10 the notification open the app not the desire Actiivity.

My Code in Notifaction is but it don't work in Android 10 or above:

        Intent intent;
        intent = new Intent(this, MainActivity.class);
        intent.putExtra("Key", true);
        intent.putExtra("Msg", message);
        // Assign channel ID
        String channel_id = "notification_channel";
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);        // Create a Builder object using NotificationCompat
        // class. This will allow control over all the flags
        NotificationCompat.Builder builder
                = new NotificationCompat
                .Builder(getApplicationContext(),
                channel_id)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setVibrate(new long[]{1000, 1000, 1000,
                        1000, 1000})
                .setOnlyAlertOnce(true);

        NotificationManager notificationManager
                = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        // Check if the Android Version is greater than Oreo

        if (Build.VERSION.SDK_INT
                >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel
                    = new NotificationChannel(
                    channel_id, "web_app",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(
                    notificationChannel);
        }

        notificationManager.notify(0, builder.build());

Not enough information to give a detailed answer. Please provide some code. Meanwhile, you can check default approach which works like a charm with Android 10: https://developer.android.com/training/notify-user/navigation

You should set channelid to builder

if (Build.VERSION.SDK_INT
                >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel
                    = new NotificationChannel(
                    channel_id, "web_app",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(
                    notificationChannel);


            //Add this Line
            builder.setChannelId(channel_id);

        }

You need to add an action eg: "com.app.action.notification" to Intent and Activity (AndroidManifest.xml) like this:

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction("com.app.action.notification");
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);        // Create a Builder object using NotificationCompat

AndroidManifest.xml

    <activity android:name=".ui.MainActivity" >
        <intent-filter>
            <action android:name="com.app.action.notification" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Happy coding!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM