我目前正在使用Firebase Cloud Messaging处理推送通知。 我实施的一切工作正常,我可以根据需要接收推送通知(无论我的应用程序处于前台,后台还是已终止) 但是我的问题是:
(为了逐步阅读,将逐步介绍我的问题)
我的应用程序框架由多个活动和许多片段组成。它从启动器活动开始,如果它检查的每个参数都很好,它将调用我的主要活动。
我的推式通知应打开一个通知片段,如果Intent用于相同的应用程序状态(背景,前景或已终止),则会执行该操作。
这是我的Firebase课
class MyFirebaseMessagingService : FirebaseMessagingService() {
val userPreferences = UserPreferences()
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be:
// Check if message contains a data payload.
remoteMessage.data.isNotEmpty().let {
val messageTitle: String = remoteMessage.data["title"] ?: "Hotel"
val message: String = remoteMessage.data["message"] ?: "You have new notification"
val groupId: String = remoteMessage.data["group_id"]?: "0"
val id: String = remoteMessage.data["id"]?: "0"
if(!userPreferences.visibleFragment.isNullOrEmpty() && groupId=="3")//IF NOT NULL OR EMPTY MUST BE CHATFRAGMENT
{
//no message since its chat notification and the chatfragment is visible
}
else
{
if(groupId=="4")
{
MainActivity.isNotificationIconVisible.postValue(true)
userPreferences.isNotificationIconVisible=true
}
sendNotification(messageTitle, message, groupId, id)
}
}
// Check if message contains a notification payload.
remoteMessage.notification?.let {
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]
// [START on_new_token]
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
override fun onNewToken(token: String) {
}
// [END on_new_token]
/**
* Handle time allotted to BroadcastReceivers.
*/
private fun handleNow() {
}
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private fun sendRegistrationToServer(token: String?) {
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private fun sendNotification(messageTitle: String, messageBody: String, groupId : String, id : String) {
val intent =
if (userPreferences.isAppDestroyed) {
Intent(this, MainActivity::class.java)
} else {
Intent(this, MainActivity::class.java)
.putExtra("id", id)
.putExtra("groupid", groupId)
.putExtra("appDestroyed", userPreferences.isAppDestroyed)
}
val pendingIntent =
if(userPreferences.isAppDestroyed)
{
TaskStackBuilder.create(this).run {
addNextIntentWithParentStack(intent)
editIntentAt(0).putExtra("id", id).putExtra("groupid", groupId).putExtra("appDestroyed", userPreferences.isAppDestroyed)
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
else
{
PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
val mode = applicationContext?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK)
val channelId = "1"
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
when (mode) {
Configuration.UI_MODE_NIGHT_YES -> {
notificationBuilder.setSmallIcon(R.drawable.ic_notification_icon_from_asset)
notificationBuilder.setColor(Color.TRANSPARENT)
}
Configuration.UI_MODE_NIGHT_NO -> {
notificationBuilder.setSmallIcon(R.drawable.ic_notification_icon_from_asset)
notificationBuilder.setColor(Color.GRAY)
}
else -> {
notificationBuilder.setSmallIcon(R.drawable.ic_notification_icon_from_asset)
notificationBuilder.setColor(Color.TRANSPARENT)
}
}
} else {
notificationBuilder.setSmallIcon(R.mipmap.ic_notification_icons)
}
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(1, notificationBuilder.build())
}
}
MainActivity onNewIntent乐趣 (片段交易代码已注释,经过测试)
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
val data = intent?.extras
val groupId = data?.getString("groupid")
val id = data?.getString("id")
if (data != null) {
if (groupId == "4") {
val bundle = bundleOf("pushNotificationId" to id)
navController.navigate(R.id.notificationDetailsFragment, bundle)
/*val fragment = NotificationDetailsFragment()
fragment.arguments=bundle
val ft = supportFragmentManager.beginTransaction()
ft.replace(R.id.main_nav_fragment, fragment)
ft.commit()*/
}
else return
}
return
}
}
LauncherActivity
class LauncherActivity : AppCompatActivity(){
private val vm: LauncherViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
injectFeature()
// set observables
vm.isSplashScreenShown.observe(this, Observer {
finish()
when (it) {
true -> startActivity(Intent(this, MainActivity::class.java).putExtra("id",intent?.extras?.getString("id"))
.putExtra("groupid",intent?.extras?.getString("groupid")))
false -> startActivity(Intent(this, WelcomeActivity::class.java))
}
})
}
}
以及我的清单的一部分(如果需要)
<activity
android:name=".presentation.launcher.LauncherActivity"
android:label="@string/app_name"
android:theme="@style/BaseAppTheme.NoActionBar.SplashScreenTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".presentation.welcome.WelcomeActivity"
android:screenOrientation="portrait"
android:theme="@style/BaseAppTheme.NoActionBar.TransparentTheme"
android:windowSoftInputMode="adjustResize" />
<activity
android:name=".presentation.error.ErrorActivity"
android:screenOrientation="portrait"
android:theme="@style/BaseAppTheme.NoActionBar.TransparentTheme" />
<activity
android:name=".presentation.main.MainActivity"
android:theme="@style/BaseAppTheme.NoActionBar.TransparentTheme"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:parentActivityName=".presentation.launcher.LauncherActivity"/>
我很乐意像Whatsapp一样完成它(即使像我对我的应用程序那样重复操作,whatsapp也会打开聊天记录)
非常感谢您,如果还需要其他内容或者我需要重新格式化我的问题,请告诉我这样做。