为什么当我的应用被杀死并在android中关闭时,我的通知无法在后台运行

时间:2018-07-02 05:43:30

标签: android firebase push-notification kotlin

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

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())

}

2 个答案:

答案 0 :(得分:1)

如果您收到通知,请检查您的logcat。您的通知是否显示在系统托盘中?

答案 1 :(得分:0)

There are two types of Message types

  1. 通知消息
  

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

  1. 数据消息
  

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

as per my comment

您需要在数据消息

中发送通知数据

示例格式

{
  "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();