当我从One Plus 3T上的最新应用程序中滑动应用程序时,我无法从Google功能接收Pus通知,但是它在Nexus模拟器上运行良好。
这是我的谷歌函数代码
//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// Listens for new messages added to messages/:pushId
exports.pushNotification = functions.database.ref('notificationRequests/{pushId}').onWrite( event => {
console.log('Push notification event triggered');
// Grab the current value of what was written to the Realtime Database.
var valueObject = event.data.val();
// Create a notification
const payload = {
data: {
title:"New Reminder",
body: "You have a new Reminder" ,
sound: "default"
},
};
//Create an options object that contains the time to live for the notification and the priority
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic(valueObject.receiverUID, payload, options)
.then(function(response){
console.log("Succesfull",response);
})
.catch(function(error){
console.log("Error sending message",error);
})
});
这是FCM回拨服务代码,功能,OnMessageReceived
public class FCMCallbackService extends FirebaseMessagingService {
private static final String TAG = "FCMCallbackService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
/*Log.d(TAG, "From:" + remoteMessage.getFrom());
Log.d(TAG, "Message Body:" + remoteMessage.getNotification().getBody());
//sendNotification(remoteMessage.getNotification());
if (remoteMessage == null)
return;
if (remoteMessage.getNotification() != null) {
Log.i(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
}*/
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.i(TAG, "Data Payload: " + remoteMessage.getData().toString());
String DataTitle = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
Log.i(TAG, "Data Payload: " + DataTitle);
sendNotification(DataTitle,body);
// sendNotification(remoteMessage.getData().toString());
}}
private void sendNotification(String title,String body) {
int color = getResources().getColor(R.color.colorPrimary);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setColor(color)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
清单中的服务
<service android:name=".FCMCallbackService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
这是我的依赖项,我正在使用旧版本的 FIrebase 消息传递,因为出于某种原因,如果我使用 10.4.1,我会收到错误"无法解析.." 。(是的,我已经更新了谷歌播放服务和存储库(
compile 'com.google.firebase:firebase-auth:9.0.2'
compile 'com.android.support:design:25.1.0'
compile 'com.google.firebase:firebase-database:9.0.2'
testCompile 'junit:junit:4.12'
compile 'com.android.support:gridlayout-v7:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.google.firebase:firebase-messaging:9.0.1'
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'com.firebaseui:firebase-ui-database:0.4.1'
您需要在 one plus 设置中将您的应用列入白名单。中国制造商阻止应用程序在后台运行。您将在小米,vivo,金立,Oppo等中面临类似的情况。