首页 > 解决方案 > ionic - firebase push only work with opened app?

问题描述

I've implemented FireBaseX in my Ionic 4 app to send remote notifications.

this.platform.ready().then(() => {
  let platforms = this.platform.platforms();

  if(platforms.includes('ios') || platforms.includes('android') || platforms.includes('mobile')){

    this.firebase.getToken().then(token => {
      console.log(`FIREBASE TOKEN ${token}`);
      if(platforms.includes('ios')) this.firebase.grantPermission();
    });

    this.firebase.onMessageReceived().subscribe(data => {
      console.log('FIREBASE MESSAGE', data);
    });

  }

});

So when I open the app in iOS, it asks for permissions correctly.

Then I send test message and the console.log() appears correctly.

2020-02-05 17:35:38.282123-0300 Parkaz[86464:2354851] didReceiveMessage: {
    "collapse_key" = "com.myapp.app";
    from = 678323471xxxx;
    notification =     {
        body = teste;
        e = 1;
        tag = "campaign_collapse_key_5659280550157990837";
        title = teste;
    };
}

However, if the app is not opened, nothing happens. The notification banner doesn't appear, no badge, no sound... nothing...

Am I doing something wrong?

标签: firebasecordovaionic-framework

解决方案


您可以使用邮递员测试您的通知。

POST : https://fcm.googleapis.com/fcm/send

标题

Content-Type:application/json
Authorization:key=AIzaXXXXXXXXXX

身体

安卓

{ 
   "notification": {
      "title": "Your Notification Title", 
      "body": "This is Message",
   },
   "to" : FIREBASE TOKEN,
   "data": { 
      "content-available": 1,
      "foreground": false,
      "clickAction": "/chat"
   }
}

iOS

{ 
   "to" : FIREBASE TOKEN,
   "data": {
      "title": "Your Notification Title", 
      "body": "This is Message",
      "content-available": 1,
      "foreground": false,
   }
}

推荐阅读