android 设置特殊活动PendingIntent

bq9c1y66  于 2023-02-17  发布在  Android
关注(0)|答案(1)|浏览(114)

我想打开特殊活动时,应用程序关闭,用户点击通知。但特殊没有打开。当用户将点击通知,我想打开TestActivity,但不是这个MainActivity打开所有的时间。
在清单中我有这个代码:

<service
            android:name=".services.firebase.notification.AppFirebaseMessaging"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
.
.
.
<activity
            android:name=".MainActivity"
            android:exported="true"
            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>
.
.
.
 <activity android:name=".TestActivity"
            android:launchMode="singleTask"
            android:taskAffinity=""
            android:excludeFromRecents="true"/>

这是我的AppFirebaseMessaging

class AppFirebaseMessaging : FirebaseMessagingService() {

 override fun onMessageReceived(remoteMessage: RemoteMessage) {
// here parsing RemoteMessage object, 
sendNotification("title", "description")
}

private fun sendNotification(title: String, description: String) {
val random = System.currentTimeMillis().toInt()
        val notificationBuilder: NotificationCompat.Builder = notificationBuilder(title, description)

        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as? NotificationManager

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager?.createNotificationChannel(getChannel(title))
        }

        notificationManager?.notify(random, notificationBuilder.build())

}

private fun notificationBuilder(title: String, description: String): NotificationCompat.Builder {
        val random = System.currentTimeMillis().toInt()
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        val notifyIntent = Intent(this, TestActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val notifyPendingIntent = PendingIntent.getActivity(
            this, 0, notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
        val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, random.toString())
            .setSmallIcon(R.drawable.icon_notification)
            .setContentTitle(title)
            .setContentText(description)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .apply {
                setContentIntent(notifyPendingIntent)
            }

        return notificationBuilder
    }
}

当用户点击通知时,主活动将打开,不了解原因

ojsjcaue

ojsjcaue1#

点击通知将始终打开具有以下Intent过滤器属性的Activity。

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

如果您无法将“TestActivity”设置为Main Activity,您可以尝试以下操作。
将MainActivity保留为Main。在MainActivity中将启动模式设置为singleTop。

<activity
        android:name=".MainActivity"
        android:launchMode="singleTop"/>

然后重写MainActivity内的“onNewIntent”,

@Override
    protected void onNewIntent(@NonNull Intent intent) {
     super.onNewIntent(intent);
  
    // here send intent to open TestActivity

    }

相关问题