java - 将通知点击数据发送到子 Activity

标签 java android android-intent android-notifications android-pendingintent

我有两个 Activity ,一个称为 SplashActivity,它是启动器和应用程序的主要 Activity ,第二个称为 MainActivity,它是 SplashActivity 的子 Activity ,基于某个事件,我创建了一个通知,当单击通知时我想要MainActivity 打开并额外发送数据。 我的问题是我已经尝试了所有出现在我面前的解决方案,但没有任何效果。

这是我当前的代码:

NotificationChannel channel = new NotificationChannel(String.valueOf(12), name, NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription(description);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
    notificationManagerCompat.createNotificationChannel(channel);

    // Create an Intent for the activity you want to start
    Intent notifyIntent = new Intent(getApplicationContext(), SplashActivity.class);
    notifyIntent.putExtra("notify", "notify");
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // Create the PendingIntent
       PendingIntent notifyPendingIntent = PendingIntent.getActivity(
            this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
                    | PendingIntent.FLAG_ONE_SHOT);


    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, String.valueOf(12))
            .setSmallIcon(R.drawable.logo)
            .setContentTitle(title)
            .setContentText(body)
            .setVibrate(new long[]{0, 100, 1000, 300})
            .setAutoCancel(true)
            .setContentIntent(notifyPendingIntent)
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // notificationId is a unique int for each notification that you must define
    notificationManagerCompat.notify(12, notification);

list 中的两个 Activity 声明:

    <activity
        android:name=".home.activities.MainActivity"
        android:parentActivityName=".SplashActivity"
        android:screenOrientation="portrait"
        tools:ignore="LockedOrientationActivity" />

    <activity
        android:name=".SplashActivity"
        android:screenOrientation="portrait"
        tools:ignore="LockedOrientationActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

有人有什么解决办法吗?为什么我当前的代码不起作用?

请帮助我,我已经工作了两天才能实现此目的。

最佳答案

启动 MainActivity 时您忘记了 putExtra 到 Intent,您只显示日志。试试这个

        Intent i = new Intent(this, destinationActivity);
        i.putExtras(getIntent());
        Log.e(TAG, "navigateTo: " + getIntent().getStringExtra("notify"));
        startActivity(i);
        finish();

关于java - 将通知点击数据发送到子 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61911625/

相关文章:

java - WAS 瘦客户端部署

java程序疑惑

android - 如何在 Activity 之间传递对象?

android - 隐藏应用程序图标并运行它

java - 更新 ADT 后无法为按钮设置 onclicklistener

android - 如何获得 PendingIntent.getActivity 的结果

java - 安卓日历 : first and last day of week -> zero month

java - 延迟一段时间后跳转到方法时出错

从文件发送短信的Android应用程序?

android - Rxjava Observe 流作为特定时间的列表