我想在用戶收到應用程序通知時自動啓動應用程序,在啓動器圖標上不會有點擊操作。如何在收到通知[FCM]時自動啓動android應用程序?
回答
在您的onMessageReceived()
方法中,您可以嘗試添加您的startActivity(intent)
代碼。這樣,當應用程序收到FCM消息時,它會啓動應用程序。像這樣...
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
startActivity(new Intent(this, MainActivity.class));
}
}
從Activity上下文外部調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。得到這個錯誤 –
onMessageReceived不會調用時,應用程序是背景所以這將反正不幫我 –
感謝您的回覆人甚至你的線程也是我的解決方案的一部分:) –
方法onMessageReceived()正常工作時,應用程序在後臺,如果通知正文不包含「通知」參數。所有數據都應粘貼在「數據」中。像這樣:
{
"to":"token",
"priority":"high",
"data": {
"title": "Carmen",
"text": "Популярные новости за сегодня!",
etc..
}
}
然後你可以在你的代碼中解析它,並在通知中顯示標題和文本。
例如:
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
Log.d("FirebaseService", "Receive notification: ${remoteMessage?.data}")
remoteMessage?.let { showNotification(remoteMessage) }
}
private fun showNotification(remoteMessage: RemoteMessage) {
val notificationModel: NotificationModel
= getNotificationModelFromMessageData(remoteMessage.data)
val intent = Intent(this, SplashActivity::class.java)
intent.putExtra(NOTIFICATION_ARGUMENT, notificationModel)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(
this, NOTIFICATION_RECEIVE_REQUEST_CODE,
intent, PendingIntent.FLAG_ONE_SHOT)
val defaultNotificationSound: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification: Notification
= NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(notificationModel.title)
.setContentText(notificationModel.text)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(defaultNotificationSound)
.build()
val notificationManager: NotificationManager
= getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID, notification)
}
private fun getNotificationModelFromMessageData(jsonData: MutableMap<String, String>): NotificationModel {
return NotificationModel(
jsonData[TITLE_PARAMETER] as String,
jsonData[TEXT_PARAMETER] as String,
jsonData[DAYS_PARAMETER] as String,
jsonData[MESSAGE_ID] as String)
}
希望它能幫助!
感謝您的答覆我從FCM數據像這樣{ Key = value}在onMessageReceived中如何實現你顯示的JSON格式.. –
最後我解決了我的問題謝謝這是因爲我沒有使用任何REST客戶端,所以數據格式是{key = value}當應用程序在前臺工作 –
- 1. Android應用程序在關閉時未收到FCM通知
- 2. 應用程序關閉時未收到Android FCM推送通知
- 3. 如何停止在android中每次啓動應用程序時收到通知?
- 4. 當它接收到遠程通知時自動啓動iPhone應用程序
- 5. 如何在警報通知中自動啓動應用程序?
- 6. Pushover通知收到時啓動應用程序
- 7. 收到通知時重新啓動應用程序
- 8. 當收到GCM通知時啓動應用程序
- 9. 如何在某個應用程序啓動時得到通知?
- 10. Android,使用parse.com推送通知,自動啓動應用程序
- 11. Android:應用程序啓動通知
- 12. Android應用程序在C#中收到FCM推送通知時崩潰
- 13. 重新啓動Android設備,在FCM通知接收上重新啓動兩次
- 14. 如何接收應用程序已啓動的通知?
- 15. 如何組FCM通知時應用程序是由Android
- 16. Cordova Android應用程序不會收到來自服務器端的FCM通知
- 17. 如何在Google Glass啓動時自動啓動應用程序?
- 18. 如何在Windows啓動時自動啓動應用程序?
- 19. 如何在應用程序啓動時自動啓動Delayed_Job?
- 20. 在啓動應用程序時收到消息「無法啓動應用程序」
- 21. 設備上收到的GCM通知未啓動應用程序
- 22. 在應用程序啓動時觸發Android通知
- 23. 如何在應用程序啓動時「自動」啓用GPS?
- 24. 如何在Android中啓動Hotspot時自動啓動我的應用程序
- 25. 如何在Android設備啓動時自動啓動Adobe AIR應用程序?
- 26. 當收到推送通知時自動打開供應商應用程序(自動'按'通知)
- 27. 如何在ClickOnce應用程序啓動時通知Web應用程序
- 28. 應用程序啓動時自動啓動Android服務
- 29. 當啓動完成時自動啓動應用程序android
- 30. 如何使用自動啓動在系統啓動時啓動應用程序?
[看這裏](http://stackoverflow.com/a/30090042/681929) – nobalG
不工作我檢查了這個 –
哥們跟着這個https://answers.madewithmarmalade.com/questions/16205/launch-application- from-notification-android-edk.html –