在某些设备中从最近的应用程序中滑动应用程序时,未从 Google 功能接收到推送通知
Posted
技术标签:
【中文标题】在某些设备中从最近的应用程序中滑动应用程序时,未从 Google 功能接收到推送通知【英文标题】:Push notificatons not receiving from Google functions when App is swiped off from recent Apps in certain devices 【发布时间】:2017-08-07 20:56:30 【问题描述】:当我在我的 One Plus 3T 上从最近的应用程序中滑动应用程序时,我无法接收来自 Google 功能的 Pus 通知,但是它在 Nexus Emulators 上运行良好。
这是我的 Google 函数代码
//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 Call backService 代码,其中包含 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,我会收到错误“无法解析..”。(是的,我已经更新了 Google Play 服务和存储库)
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'
【问题讨论】:
Android app not receiving Firebase Notification when app is stopped from multi-task tray的可能重复 【参考方案1】:您需要在一加设置中将您的应用列入白名单。中国制造商阻止应用程序在后台运行。你在小米、vivo、金立、OPPO等都会遇到类似的情况。
【讨论】:
以上是关于在某些设备中从最近的应用程序中滑动应用程序时,未从 Google 功能接收到推送通知的主要内容,如果未能解决你的问题,请参考以下文章