Functions发送的弹出通知

Functions发送的弹出通知

本文介绍了Android应用程序不显示从Cloud Functions发送的弹出通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与firebase实时数据库创建了一个应用聊天,我正在执行添加朋友步骤,我想在用户单击添加朋友时显示通知.我使用了Firebase函数.一切正常,但我的设备仍未显示通知.我真的不知道.有人可以帮我吗?

I create an app chat with firebase realtime db, I'm doing add friend step, I want to show a notification when user click add friend. I used the firebase function. Everything works fine but my device still not show notification. I really don't know while. Can someone help me?

要在firebase上部署的我的index.js:

My index.js to deploy on firebase:

'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((data, context) => {

const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have a notification to send to: ', user_id);

const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

return deviceToken.then(result=>{
const token_id=result.val();

const payload={
notification:{
title: "FRiend Request",
body: "You have received a friend request",
icon:"default"}
};

return admin.messaging().sendToDevice(token_id,payload).then(response =>{
console.log('This was notification feature');
return true;});
});
});

在Firebase功能控制台上(它仍然发送了通知)

And on firebase function console (It still has a notification sent)

推荐答案

您需要Firebase Messenger服务类.

You need a firebase messenging service class.

创建一个名为 MyFirebaseMessagingService 的类,该类将接收通知并对其进行解码.

create a class called MyFirebaseMessagingService which will receive the notification and decode it.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    private static final String CHANNEL_ID = "MyFirebaseMessagingService";

    String title;
    String message;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.

        title = "You have a new notification";
        message = remoteMessage.getNotification().getBody();
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("You have a new notification")
                .setContentText(remoteMessage.getNotification().getBody());


        int notificationId = (int) System.currentTimeMillis();
        // Issue the notification.

        NotificationManager notification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        assert notification != null;
        notification.notify(notificationId, mBuilder.build());


        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(notificationId, mBuilder.build());

        createNotificationChannel();

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());


    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = title;
            String description = message;
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);


            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            assert notificationManager != null;
            notificationManager.createNotificationChannel(channel);
        }
    }

    }

在您的 Manifest.xml 中添加元数据,如下所示:

Add a meta-data inside your Manifest.xml like the followiing

        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_launcher" />

还将以下消息传递服务添加到您的xml

Also add the following messaging service to your xml

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

这篇关于Android应用程序不显示从Cloud Functions发送的弹出通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:33