Push notifications not received after device restart

plugins

#1

I am using Nativescript with Angular 4 and recently i noticed that I cant receive push notifications if I restart my device (and also don’t open the app after that). I am using letter by letter the installation of nativescript-plugin-firebase from Eddy Verbruggen. I have only custom rom devices in my disposal but i dont think that matter because other apps like facebook, whatsapp or gopro can send me push notifications even after restart. Is this a misconfiguration from my app or is it a limitation of nativescript?

PS: I get push notifications even if i kill the app but i have to wait ~20 seconds after i kill it to receive a push notification. I always receive push notifications if i open at least 1 time my app after device restart.


#2

Hi @krisidmisso
You should setup a Broadcast Receiver that listens to
android.intent.action.BOOT_COMPLETED
And when that happens, perform a firebase init.

Use this link
https://docs.nativescript.org/cookbook/application#registering-a-broadcast-receiver-android
To learn how to set it up.

Hope that helps :slight_smile:


#3

@multishiv19 Thank you about that, but im not able to make it work:

if (app.android) {
app.android.registerBroadcastReceiver(android.intent.action.BOOT_COMPLETED,
onReceiveCallback => {
that.firebaseINIT();
});
}
I am getting -> app/views/home.component.ts(100,55): error TS2339: Property ‘intent’ does not exist
on type ‘typeof android’.

I have installed tns-platform-declarations and inserted the android reference in references.d.ts. am i missing something??


#4

@krisidmisso
Please use android.content.Intent.ACTION_BOOT_COMPLETED

The one I mentioned earlier was Java code, from SO. In NativeScript it looks a little different.


#5

@multishiv19 I did that and now im not able to register device token. also inserted a console log inside broadcast register and i didnt get it. the code im using is this:

constructor(){
.....

let that = this;
    app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BOOT_COMPLETED,
      function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
        console.log('broadcast ok');
        that.firebaseINIT();
      });
}


firebaseINIT() {
    setTimeout(() => {
      let firebase = require("nativescript-plugin-firebase");
      console.log('firebase ok');
      firebase.init({
        onPushTokenReceivedCallback: (token) => {
          //save token to app settings storage 
          appSettings.setString('Token', token);
          if (appSettings.hasKey('Token')) {
            this.dev_token = token;
            this.platformInfo.device_token = token;
            this.registerDevice();
          }
        },
        onMessageReceivedCallback: (message) => {
          if (message.website !== undefined) {
            if (appSettings.hasKey('Login')) {
              console.log(message.website);
              dialogs.confirm({
                title: "New mention about you",
                message: "You were mentioned in " + message.website,
                okButtonText: "Open Link",
                cancelButtonText: "Cancel"
              }).then(result => {
                if (result == true) {
                  this.browseWeb(message.website);
                }
              });
            } else {
              Toast.makeText('Please login to view news about you!').show();
            }
          }
        },
        //persist should be set to false as otherwise numbers aren't returned during livesync  
        persist: false,

      }).then(function (instance) {
        console.log("-------Firebase.init done------");
      }), function (error) {
        console.log("firebase.init error: " + error);
      };
    },3000);
}

Can you please help me?


#6

don’t remove the regular firebase init which you do in your app.ts/app.js/main.ts

That firebase init, will initialize firebase when the app is opened normally.

And this firebase init, inside the broadcast receiver’s call back, can be made directly inside the callback function

if (app.android) {
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BOOT_COMPLETED,
      function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
        console.log('broadcast ok');
        let firebase = require("nativescript-plugin-firebase");
      console.log('firebase ok');
      firebase.init({
        onPushTokenReceivedCallback: (token) => {
          //save token to app settings storage 
          appSettings.setString('Token', token);
          if (appSettings.hasKey('Token')) {
            this.dev_token = token;
            this.platformInfo.device_token = token;
            this.registerDevice();
          }
        },
        onMessageReceivedCallback: (message) => {
          if (message.website !== undefined) {
            if (appSettings.hasKey('Login')) {
              console.log(message.website);
              dialogs.confirm({
                title: "New mention about you",
                message: "You were mentioned in " + message.website,
                okButtonText: "Open Link",
                cancelButtonText: "Cancel"
              }).then(result => {
                if (result == true) {
                  this.browseWeb(message.website);
                }
              });
            } else {
              Toast.makeText('Please login to view news about you!').show();
            }
          }
        },
        //persist should be set to false as otherwise numbers aren't returned during livesync  
        persist: false,

      }).then(function (instance) {
        console.log("-------Firebase.init done------");
      }), function (error) {
        console.log("firebase.init error: " + error);
      };
   });
}

Also for this broadcast receiver to get attached first, you need to open the app once.


#7

@multishiv19 I did this but no success. I can get push notifications but when i restart the device it doesnt get any notifications. Also i noticed that console doesnt output ‘broadcast ok’ message when i open the app, meaning it doesnt register it??. Is it mandatory to use firebase inside main.ts? also i am using this broadcast receiver inside my main page (home.ts) which i access every time i open the app.


#8

This console log that you are expecting, is supposed
to come after phone restarts.
And to see if it actually comes, you’ll have to read phone logs using ADB logcat/ or inside that Android studio.

I found a repo that demonstrates for to use android background services.
Hope this helps


#9

@multishiv19 I have seen that sample the moment you mentioned broadcast receivers. Also i noticed this in his manifest:

But he is using local notifications and it doesnt apply my needs. I need push notifications from firebase.
I also read about this:

but compared to above sample it didnt change any config in manifest file.

Can you please suggest me any other solution?


#10

@krisidmisso I suggest you try adding this to the manifest. That looks like the missing piece, that makes broadcast receivers work.

Note that, any code you write inside the callback of the receiver, will be triggered only when the condition of the trigger is met.

So, the regular firebase init you do, will remain the same.


#11

I tried this:

declare var android:any;
.....
export class HomeComponent {
.....
constructor(){
.....
<FIREBASE INIT CODE HERE>
.....

android.content.BroadcastReceiver.extend("com.tns.broadcastreceivers.NotificationServiceStarterReceiver", {
    onReceive: function() {
        <FIREBASE INIT CODE HERE>
    }
});

.....
}
<other methods>
}

and inside AndroidManifest.xml:

added <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

and this:


<receiver android:name="com.tns.broadcastreceivers.NotificationServiceStarterReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>

Again push notifications dont work after device restart (without opening the app) :frowning:


#12

@krisidmisso
In that case I’m not sure what else is wrong.
At my workplace, we’ll have to tackle the same issue sometime soon. If we find a solution, I’ll reply in this thread :slight_smile:


#13

I just made a test with push-plugin from nativescript and it works even after restart. The instalation was straight forward and easy, no manifest or gradle modifications. What i dont like from that plugin is the way it behaves when i receive push, and also samples are in js not angular as im working.

Please if you find any solution regarding firebase-notifications plugin dont forget me :slight_smile:
Thank you for your time here


#14

Sure thing. But I am working with NativeScript Core though :sweat_smile:
Will let you know for sure :slight_smile: