通知单击始终会打开启动器活动

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

当我单击通知时,点击操作始终会启动带有启动器活动的应用程序,即使该应用程序已经在运行。我尝试了不同的标志配置等,但没有任何帮助。有什么想法吗?

通知:

private void sendNotification(String title, String messageBody, String senderID) {
        Intent launchIntent = new Intent(this, MainActivity.class);
        launchIntent.setAction(Intent.ACTION_MAIN);
        launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        String channelId = "PushNotificationChannel"; // Create a unique channel ID
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Neview")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(messageBody)
                        )
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since Android Oreo (API level 26), notification channels are required.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "PushNotifications",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
    }

清单:

 <activity
            android:launchMode="singleTask"
            android:name="com.cybertyc.neview_android.MainActivity"
            android:exported="false"
            android:hardwareAccelerated="true"
            android:screenOrientation="portrait">
<activity
            android:name="com.cybertyc.neview_android.SplashScreen.SplashScreen_page"
            android:exported="true"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
java android xml mobile
1个回答
0
投票

You may write the code like this.

//You need create one service class.

val intent = Intent(context,ThisService::class.java)
intent.putExtra("notify",1)
val pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

//You Builder set the pendingIntent.
Builder().setContentIntent(pendingIntent)

//The ThisService will callback onStartCommand method when click notification.
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        super.onStartCommand(intent, flags, startId)

        intent?.let {
            val key = it.extras?.getInt("notify")
            when (key) {
                1 -> {
                    //You operate
                    //#########
                }
            }
        }
        return START_STICKY
    }

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