简体   繁体   中英

FCM push notification to reawaken app not effective while device slept

I ran into a problem with my flutter app where when it's minimized or the phone is put to sleep for too long (~5+ minutes), the app stops listening to firestore changes. One solution that I came across was to send push notifications to reawaken the device. While it seems to have fixed minimizing problem (the app now responds to changes), however it still suffers from the sleep problem. I noticed that the app still receives the push notifications, but the screen doesn't light up upon receiving them. Could that be why? Is there something that I can do to force the app to connect to the inte.net? I'm trying to think of a solution like sending a data payload to change the data locally, but I'm not sure if that's the optimal approach (or if it would even work). I'll post my firebase cloud function for sending messages on a doc update:

exports.sendLobbyNotificationTest = functions.firestore
    .document("Lobbies/{lobbyCode}")
    .onUpdate((change) => {
      console.log("Checking for need of push notification...");
      // const oldValue = change.before.data() || {};
      const newValue = change.after.data() || {};
      if (newValue.pushNotification == true) {
        console.log("Found a push notification request for: ",
            newValue.lobbyCode, "!");
        // Set lobby back to false
        admin.firestore().collection("Lobbies")
            .doc(newValue.lobbyCode).update({
              pushNotification: false,
            });
        return admin.messaging().sendToTopic(newValue.lobbyCode, message)
            .then((result) => {
              console.log("Message sent successfully: ", result);
              // usersRef.where("lobbyCode", "==", newValue.lobbyCode).get()
              //     .then(function(querySnapshot) {
              //       querySnapshot.forEach(function(doc){
              //       })
              //     })
            }).catch((err) => {
              console.error("Error sending message: ", err);
            });
      }
      console.log("No message needs to be sent!");
      // return dummy value to prevent error
      return 0;
    });

const message = {
  notification: {
    title: "Bzzzt!",
    body: "You've been buzzed!",
  },
};

Is there something I'm missing?

Update: I think it just doesn't work because the phone is locked, once unlocked it begins to function normally.

I think figured out why from reading here: https://developer.android.com/training/monitoring-device-state/doze-standby.html (I had trouble finding this).

Basically if I use the right priority settings for android and ios (priority & content_available) it will bring the app out of idle and listen for changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM