【问题标题】:The notification is displayed, but clicking the notification cannot jump to the page显示通知,但点击通知无法跳转页面
【发布时间】:2022-10-25 19:48:00
【问题描述】:

目标:点击按钮跳出通知,用户可以从通知跳转到另一个页面。

显示通知但点击通知无法跳转页面。

我认为PendingIntent 是错误的。

如何解决?

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());
        }

【问题讨论】:

    标签: java android notifications android-pendingintent


    【解决方案1】:

    检查这是否适合您。如android api 中所述

    您应该考虑用户的预期导航体验。因此,为了使用pendingIntentNotification 进行最佳导航,您应该执行以下操作:

    1.首先在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>
    

    现在你告诉android,当点击通知时,SecondActivity 将是MainActivity 的孩子,当用户向上导航时。

    2. 使用TaskStackBuilder 创建PendingIntent

    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    
    PendingIntent pendingIntent = TaskStackBuilder.create(MainActivity.this)
         .addNextIntentWithParentStack(intent)
         .getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE)
    

    然后可以将我们创建的PendingIntent设置为通知。现在,如果用户触摸通知,android 系统会将他导航到SecondActivity,同时将MainActivity 保留在后台堆栈中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-05
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多