Home > database >  The notification is displayed, but clicking the notification cannot jump to the page
The notification is displayed, but clicking the notification cannot jump to the page

Time:10-17

goal:Click the button to jump out of the notification, the user can jump from the notification to another page

The notification is displayed, but clicking the notification cannot jump to the page

I think the PendingIntent is wrong

how to fix it?

public class MainActivity extends AppCompatActivity {

private String CHANNEL_ID = "Coder";
NotificationManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel channel = new NotificationChannel(
                CHANNEL_ID, "DemoCode", NotificationManager.IMPORTANCE_DEFAULT);
        manager = getSystemService(NotificationManager.class);
        assert manager != null;
        manager.createNotificationChannel(channel);
    }

   
    Button btDefault,btCustom;
    btDefault = findViewById(R.id.button_DefaultNotification);
    btCustom = findViewById(R.id.button_CustomNotification);
    btDefault.setOnClickListener(onDefaultClick);

}

private final View.OnClickListener onDefaultClick = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
       
        Intent nextIntent = new Intent(MainActivity.this, secondActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, nextIntent,PendingIntent.FLAG_IMMUTABLE);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID);

        builder.setContentTitle("Notification");
        builder.setContentText("You have a new message");
        builder.setSmallIcon(R.drawable.ic_baseline_accessible_forward_24);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);


        manager.notify(1, builder.build());}

CodePudding user response:

Check if this works for you. As mentioned in android api

You should consider user's expected navigation experience. So for making the best navigation using pendingIntent with Notification, you should do as follow:

1. First define your app's hierarchy inside AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    ... >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<!-- MainActivity is the parent for SecondActivity -->
<activity
    android:name=".SecondActivity"
    android:parentActivityName=".MainActivity" />
    ...
</activity>

Now you told android that when a notification clicked, SecondActivity would be the child of MainActivity when user is navigating up.

2. Create PendingIntent using TaskStackBuilder:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);

PendingIntent pendingIntent = TaskStackBuilder.create(MainActivity.this)
     .addNextIntentWithParentStack(intent)
     .getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE)

Then you can set the PendingIntent we created to the notification. And now if user touches the notification, android system will navigate him to SecondActivity while holding MainActivity in back stack.

  • Related