Pu sh Уведомление не загружает приложение, когда оно уничтожено - PullRequest
0 голосов
/ 11 февраля 2020

Я работаю над Pu sh Манипулирование уведомлениями, я сделал части, когда приложение находится в фоновом режиме и на переднем плане, и оно работает так, как я хочу. Дело в том, что когда приложение уничтожается и когда я нажимаю на уведомление pu sh, оно ничего не делает.

FirebaseClass

 val intent: Intent =
            if (groupId == "4" && !userPreferences.isAppInBackground) {
                Intent(this, MainActivity::class.java)
                    .putExtra("id", id)
                    .putExtra("groupid", groupId)
            } else if (userPreferences.isAppDestoryed) {
                Intent(this, MainActivity::class.java)
                    .putExtra("id", id)
                    .putExtra("groupid", groupId)
                    .putExtra("isDestroyed",true)
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
            } else {
                Intent(this, MainActivity::class.java)
                    .putExtra("id", id)
                    .putExtra("groupid", groupId)
            }

        val pendingIntent =
                PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_UPDATE_CURRENT)

onNewIntent в MainActivity


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)
            }
            else return
        }
        else return

    }

И в манифесте я поставил android:launchMode="singleTop" в части Основной Деятельности. Если у кого-то есть ответ, который был бы великолепен, заранее благодарим Вас!

РЕДАКТИРОВАТЬ: весь мой построитель уведомлений

private fun sendNotification(messageTitle: String, messageBody: String, groupId : String, id : String) {

        /*if (userPreferences.isAppDestoryed) {

        }
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(
            this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT
        )*/

        val intent: Intent =
            if (groupId == "4" && !userPreferences.isAppInBackground) {
                Intent(this, MainActivity::class.java)
                    .putExtra("id", id)
                    .putExtra("groupid", groupId)
            } else if (userPreferences.isAppDestoryed) {
                Intent(this, LauncherActivity::class.java)
                    .putExtra("id", id)
                    .putExtra("groupid", groupId)
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            } else {
                Intent(this, MainActivity::class.java)
                    .putExtra("id", id)
                    .putExtra("groupid", groupId)
            }
        val pendingIntent =
        if(userPreferences.isAppDestoryed)
        {
            PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT)
        }
        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())
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...