当从某些设备中的最新应用程序中删除应用程序时,推送未通过Google功能接收的通知

时间:2017-08-07 20:56:30

标签: firebase push-notification firebase-cloud-messaging google-cloud-functions

当我从One Plus 3T上的最新应用程序中滑动应用程序时,我无法接收来自Google功能的Pus通知,但它在Nexus仿真器上完美运行。

以下是我的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 Messaging因为某些原因,如果我使用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'

1 个答案:

答案 0 :(得分:0)

您需要在一个加号设置中将您的应用列入白名单。中国制造商阻止应用程序在后台运行。你会在小米,Vivo,金立,Oppo等面临一个类似的情况。