为什么当我的应用被杀死并在android中关闭时,我的通知无法在后台运行 [英] Why my notification not work in background when my app is kill and close in android

查看:227
本文介绍了为什么当我的应用被杀死并在android中关闭时,我的通知无法在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打开我的应用程序时,通知可以正常运行,但是在后台运行时则无法运行.我希望即使在我的应用程序被杀死或关闭的情况下也可以显示通知.但这是行不通的.这是我的代码:-

When my app is open, notification works perfectly but when in background it doesn't work .I want the notification to be displayed even when my app is killed or closed in my phone. But it is not working. Here is my code :-

1)MyFirebaseMessagingService.kt:-

1) MyFirebaseMessagingService.kt :-

class MyFirebaseMessagingService : FirebaseMessagingService() {


override fun onMessageReceived(remoteMessage: RemoteMessage?) {

    showNotification(remoteMessage!!)
    Log.e("fcm", "HElloo Call")

}


@SuppressLint("WrongConstant")
private fun showNotification(message: RemoteMessage) {
    val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


        val i = Intent(this, MainActivity::class.java)
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

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

        val androidChannel = NotificationChannel("1",
                "sdccscsc", NotificationManager.IMPORTANCE_DEFAULT)
        androidChannel.enableLights(true)
        androidChannel.enableVibration(true)
        androidChannel.lightColor = Color.GREEN
        androidChannel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE

        val builder = NotificationCompat.Builder(this, "1")
                .setAutoCancel(true)
                .setContentTitle(message.data["title"])
                .setContentText(message.data["message"])
                .setSmallIcon(R.drawable.main_logo)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentIntent(pendingIntent)

        manager.createNotificationChannel(androidChannel)
    }


    val i = Intent(this, MainActivity::class.java)
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

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

    val builder = NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle(message.data["title"])
            .setContentText(message.data["message"])
            .setSmallIcon(R.drawable.main_logo)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pendingIntent)
    manager.notify(0, builder.build())

}

推荐答案

有两种类型的消息类型

  1. 通知消息

FCM代表客户端应用程序自动将消息显示给最终用户设备.通知消息具有一组预定义的用户可见键和一个自定义键值对的可选数据有效载荷.

FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

  1. 数据消息

客户端应用负责处理数据消息.数据消息仅具有自定义键值对.

Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

您需要在数据消息

示例格式

{
  "message":{
    "token":"your token",
    "data":{
      "key1" : "value1",
      "key2" : "value2",
      "key3" : "value4",
    }
  }
}

编辑

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

    Intent intent = new Intent(this, YourActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        intent.putExtra("ID",object.getString("data"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);


    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "Your_channel";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH);

        notificationChannel.setDescription("Description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

    notificationBuilder.setAutoCancel(true)
            .setColor(ContextCompat.getColor(this, R.color.colorDarkBlue))
            .setContentTitle(getString(R.string.app_name))
            .setContentText(remoteMessage.getNotification().getBody())
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_notification)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setPriority(Notification.PRIORITY_MAX);

    notificationManager.notify(1000, notificationBuilder.build());
    notificationManager.cancelAll();

这篇关于为什么当我的应用被杀死并在android中关闭时,我的通知无法在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆