我正在尝试使用Firebase函数向使用Java编写的Android应用程序中的用户发送通知,但我仍然在Firebase功能控制台日志中收到此错误:
Error: Messaging payload contains an invalid value for the "notification.title" property. Values must be strings.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:130:23)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:486:27
at Array.forEach (native)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:483:32
at Array.forEach (native)
at Messaging.validateMessagingPayload (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:476:21)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:313:37
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
这是我的功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref('/messages/send').onWrite(
event => {
console.log('Push notification event triggered');
event.data.adminRef.parent.once('value').then((snapshot) => {
var valueObject = snapshot.val();
const payload = {
notification: {
title: JSON.stringify(valueObject.title),
body: JSON.stringify(valueObject.message),
click_action: ".WebsiteActivityNotification",
sound: "default"
},
data: {
title: JSON.stringify(valueObject.title),
message: JSON.stringify(valueObject.message),
link: JSON.stringify(valueObject.link)
}
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24 //24 hours
};
return admin.messaging().sendToTopic("notifications", payload, options);
});
});
以下是我的Firebase数据库结构:
/messages
link: "..."
message: "..."
title: "..."
send: "..."
这是我的服务:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FCMService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String notificationTitle = null, notificationBody = null;
String dataTitle = null, dataMessage = null, dataLink = null;
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("message"));
dataTitle = remoteMessage.getData().get("title");
dataMessage = remoteMessage.getData().get("message");
dataLink = remoteMessage.getData().get("link");
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
notificationTitle = remoteMessage.getNotification().getTitle();
notificationBody = remoteMessage.getNotification().getBody();
}
sendNotification(notificationTitle, notificationBody, dataTitle, dataMessage, dataLink);
}
private void sendNotification(String notificationTitle, String notificationBody, String dataTitle, String dataMessage, String dataLink) {
Intent intent = new Intent(this, WebsiteActivityNotification.class);
intent.putExtra("title", dataTitle);
intent.putExtra("message", dataMessage);
intent.putExtra("link", dataLink);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notificationTitle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
.setContentText(notificationBody)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
在Manifest中打开标记的活动:
<activity
android:name=".WebsiteActivityNotification"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name=".WebsiteActivityNotification" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Manifest中的服务标签:
<service android:name=".FCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
仍然没有工作...... 每个答案都受到欢迎。非常感谢你!