无法再次点击Android FCM通知

时间:2019-01-16 02:59:21

标签: android firebase kotlin firebase-cloud-messaging android-notifications

我的通知有问题。

第一种方法-打开我的应用程序,然后我第一次从FCM推送通知,单击notif,执行了我的代码,第二次再次推送,单击了notif,我的代码仍然执行,我尝试了更多时间做得很好。

第二种方式-我的应用程序仍然关闭,我第一次从FCM推送notif,然后单击notif,这样我的应用程序将打开并执行我的代码,然后我再次再次推送notif,我的代码未执行,我再尝试更多时间仍然无法正常工作。

直到我轻扫我的应用程序,然后重试,问题仍然存在。

这是我的代码

class MyFirebaseMessagingService : FirebaseMessagingService() {
    var userInformation = AccountTableDao()
    val sp: SharedPreferences by lazy { SharedPreference.instance }
    val gad: GetAccountData by lazy { AccountDatabase.instance }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        Log.d("firebase", "INSTANCE ID ===> $token")
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        remoteMessage?.let {
            sendNotification(it.data)
        }
    }

    private fun sendNotification(messageData: Map<String, String>) {

        var status = messageData.get("status")
        val uuid = messageData.get("uuid")
        val restaurantId = messageData.get("restaurantId")

        val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val titleText = messageData.get("title").toString()
        val contentText = messageData.get("body").toString()

        val intent = Intent(applicationContext, RestaurantMenuActivity::class.java)
        intent.putExtra(Argument.NOTIF_UUID, uuid)
        intent.putExtra(Argument.NOTIF_RESTAURANT_ID, restaurantId)
        intent.putExtra(Argument.NOTIF_STATUS, status)
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        val pendingIntent = PendingIntent.getActivity(applicationContext,System.currentTimeMillis().toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val notificationChannel = NotificationChannel("appety", "appety_notification", NotificationManager.IMPORTANCE_HIGH)

            notificationManager .createNotificationChannel(notificationChannel)
            notificationManager.notify(System.currentTimeMillis().toInt(), NotificationCompat.Builder(this, "1")
            .setChannelId(System.currentTimeMillis().toString())
            .setContentTitle(titleText)
            .setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
            .setContentText(contentText)
            .setSmallIcon(R.drawable.ic_check_black_24dp)
            .setLargeIcon( BitmapFactory.decodeResource(applicationContext.resources, R.drawable.logo_appety))
            .setContentIntent(pendingIntent)
            .setOngoing(false)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setColor(ContextCompat.getColor(applicationContext, R.color.colorBonAppety))
            .build())
        }
        else{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                notificationManager .notify(System.currentTimeMillis().toInt(), NotificationCompat.Builder(this, System.currentTimeMillis().toString())
                .setContentTitle(titleText)
                .setStyle (NotificationCompat.BigTextStyle().bigText(contentText))
                .setContentText(contentText)
                .setSmallIcon(R.drawable.ic_check_black_24dp)
                .setLargeIcon( BitmapFactory.decodeResource(applicationContext.resources, R.drawable.logo_appety))
                .setContentIntent(pendingIntent)
                .setOngoing(false)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setColor(ContextCompat.getColor(applicationContext, R.color.colorBonAppety))
                .build())
            } else {
                notificationManager .notify(System.currentTimeMillis().toInt(), NotificationCompat.Builder(this, "1")
                .setContentTitle(titleText)
                .setStyle (NotificationCompat.BigTextStyle().bigText(contentText))
                .setContentText(contentText)
                .setContentIntent(pendingIntent)
                .setOngoing(false)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .build())
            }
        }
    }

    companion object {
        private val TAG = "token"
    }
}

和清单

<service
        android:name=".service.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

为什么Pending Intent不起作用以及解决方法。 对不起,我的英语和问候。

1 个答案:

答案 0 :(得分:0)

创建通知时,应尝试使用setAutoCancel()方法。

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder summary = new NotificationCompat.Builder(this);
summary.setAutoCancel(true);

如果您将意图传递给通知,请使用此

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_ONE_SHOT);