Android应用程序不显示从Cloud Functions发送的弹出通知

时间:2018-08-31 16:37:38

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

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

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

'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功能控制台上(它仍然发送了通知)

My Firebase Console Function

1 个答案:

答案 0 :(得分:1)

您需要一个Firebase Messenger服务类。

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

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内部添加元数据,如下所述

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

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

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