即使应用程序在后台或被杀死,也发送由 onDataChange 触发的通知

时间:2021-07-22 14:45:30

标签: android firebase-realtime-database push-notification firebase-cloud-messaging android-notifications

我正在尝试向用户发送通知,该通知将通过使用 ValueEventListener() 方法检查特定值来触发。这非常有效,我知道这仅在应用程序运行时才有效。一旦应用程序被杀死,监听器就会被销毁并且通知显然不会显示。

经过数小时的谷歌搜索和在线阅读后,我发现即使应用未运行,Firebase Cloud Messaging 也可用于发送通知。我在这里的主要困难是如何实现它并在通过使用 ValueEventListener() 方法检查特定值触发时仅向一个特定用户发送通知。

以下代码在应用程序运行/前台运行时完成其工作。当应用程序被杀死/后台使用 FCM 触发通知时,如何使用 ValueEventListener() 方法或从 firebase 实时数据库执行相同的操作。我意识到的矛盾是当我的应用程序被杀死时我的方法和侦听器没有运行,所以我需要另一个解决方案。

        dbRef.child("Users").child(userID).child("Accepted").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot3) {
                if (snapshot3.exists()) {
                    long[] pattern = {200, 400, 300, 300, 300, 300};
                    NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(MainActivity.this)
                            .setSmallIcon(R.drawable.ic_stat_fastfood)
                            .setColor(getResources().getColor(R.color.yellowTheme))
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.dish))
                            .setContentTitle("Order Accepted")
                            .setLights(getResources().getColor(R.color.yellowTheme), 500, 5000)
                            .setVibrate(pattern)
                            .setDefaults(Notification.DEFAULT_SOUND)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText("Your order has been accepted by the kitchen. Your meal will reach you on time."))
                            .setContentText("Your order has been accepted by the kitchen.")
                            .setAutoCancel(true);

                    Intent intent = new Intent(MainActivity.this, MainActivity.class);   //keep intent empty when we want to do nothing when clicked on notif.
                    PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                    dbRef.child("Users").child(userID).child("Accepted").removeValue();
                    mbuilder.setContentIntent(pi);

                    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                    String CHANNEL_ID = "my_channel_01";
                    mbuilder.setChannelId(CHANNEL_ID);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        CharSequence name = getString(R.string.app_name);
                        int importance = NotificationManager.IMPORTANCE_HIGH;
                        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                        assert mbuilder != null;
                        manager.createNotificationChannel(mChannel);
                    }

                    manager.notify(0, mbuilder.build());

                    notified = true;
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

0 个答案:

没有答案