从通知点击发送数据到子活动

问题描述 投票:0回答:2

我有两个活动,一个活动称为SplashActivity,它是应用程序的启动器和主要活动,第二个活动名为MainActivity,它是SplashActivity的子活动,基于某个事件,我创建了一个通知,当单击通知时,我想要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);

清单中的两个活动声明:

    <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>

任何人有什么解决方案?以及为什么我当前的代码不起作用?

请帮助我,我已经工作了两天,才能正常工作。

java android android-intent android-notifications android-pendingintent
2个回答
0
投票

启动MainActivity时,您忘记将putExtra设置为意图,仅显示日志。试试这个

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

0
投票

经过一段时间尝试其他解决方案后,我不知道问题出在哪里。

但是我使用的是此代码:

if (getIntent().getExtras() != null)
        getIntent().getExtras().keySet().iterator().forEachRemaining(new Consumer<String>() {
            @Override
            public void accept(String s) {
                Log.e(TAG, "accept: " + s);
            }
        });

打印从通知中打开活动时我拥有的额外键,我看到了一组唯一的键,当我从通知中打开时,我将其中一个用作特定操作的触发器。

© www.soinside.com 2019 - 2024. All rights reserved.