【问题标题】:receiving push notification after realtime database value changes在实时数据库值更改后接收推送通知
【发布时间】:2018-04-13 13:18:29
【问题描述】:

每当节点更新到 FCM 实时数据库时,我想向用户发送推送通知。我的 FCM 函数触发了一个通知。我可以在 FCM 日志中看到这一点。但我看不到通知在我的客户端应用程序中显示。有人能帮我吗? 我的日志如下:

在客户端,我有以下代码来接收通知: MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        String channelId = "1";
        String channel2 = "2";

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(channelId,
                    "Channel 1",NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("This is BNT");
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setShowBadge(true);
            notificationManager.createNotificationChannel(notificationChannel);

            NotificationChannel notificationChannel2 = new NotificationChannel(channel2,
                    "Channel 2",NotificationManager.IMPORTANCE_MIN);

            notificationChannel.setDescription("This is bTV");
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setShowBadge(true);
            notificationManager.createNotificationChannel(notificationChannel2);

        }

        // Get Firebase database reference
        this.mDatabase = FirebaseDatabase.getInstance().getReference().child("masterSheet");
        //FirebaseMessaging.getInstance().subscribeToTopic("pushNotifications");
        //FirebaseMessaging.getInstance().subscribeToTopic("pushNotifications");
        // Init user list
        ListView list = (ListView) this.findViewById(R.id.dataList);
        this.listAdapter = new DataListAdapter(this, R.layout.list_view_cell);
        list.setAdapter(listAdapter);
    }

MyFirebaseMessagingService.java

public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    if(MainActivity.isAppRunning){
        //Some action
    }else{
        //Show notification as usual
    }

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0 /* Request code */, notificationIntent,
            PendingIntent.FLAG_ONE_SHOT);

    //You should use an actual ID instead
    int notificationId = new Random().nextInt(60000);


    Bitmap bitmap = getBitmapfromUrl(remoteMessage.getData().get("image-url"));

    Intent likeIntent = new Intent(this,LikeService.class);
    likeIntent.putExtra(NOTIFICATION_ID_EXTRA,notificationId);
    likeIntent.putExtra(IMAGE_URL_EXTRA,remoteMessage.getData().get("image-url"));
    PendingIntent likePendingIntent = PendingIntent.getService(this,
            notificationId+1,likeIntent,PendingIntent.FLAG_ONE_SHOT);


    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

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

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        setupChannels();
    }

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
                    .setLargeIcon(bitmap)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(remoteMessage.getData().get("title"))
                    .setStyle(new NotificationCompat.BigPictureStyle()
                            .setSummaryText(remoteMessage.getData().get("message"))
                            .bigPicture(bitmap))/*Notification with Image*/
                    .setContentText(remoteMessage.getData().get("message"))
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .addAction(R.drawable.ic_favorite_true,
                            getString(R.string.notification_add_to_cart_button),likePendingIntent)
                    .setContentIntent(pendingIntent);

    notificationManager.notify(notificationId, notificationBuilder.build());

}

Node.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref('/masterSheet/{pushId}').onWrite( event => {
console.log('Push notification event triggered');
    const payload = {
        notification: {
            title: 'App Name',
            body: "New message",
            sound: "default"
        },
        data: {
            title: "New Title",
            message:"New message"
        }
    };
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24 //24 hours
    };
return admin.messaging().sendToTopic("notifications", payload, options);
});

【问题讨论】:

  • 您是否调试过代码以查看是否调用了 onMessageReceived 方法?另外,看看你的云功能代码会很有用:)
  • On 收到的消息没有被调用。我可以看到使用断点。
  • 代码没有输入onMessage收到的任何原因?

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


【解决方案1】:

您似乎没有订阅“通知”主题:

FirebaseMessaging.getInstance().subscribeToTopic("notifications");

【讨论】:

  • 感谢 Levi,它成功了。我现在可以收到通知了 :)
  • 很高兴我能帮助你,别忘了标记为答案,这样它可以帮助别人;)
猜你喜欢
  • 1970-01-01
  • 2013-01-14
  • 2017-10-24
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 2019-11-08
相关资源
最近更新 更多