havesource / cordova-plugin-push

Register and receive push notifications
MIT License
148 stars 283 forks source link

Does not work on iOS #150

Open george19gt opened 2 years ago

george19gt commented 2 years ago

Bug Report

The device is successfully registered but sending a notification with APNs does not work. If it works if I use FCM doing tests with firebase I managed to get it to work, but with APN there is no way. Android works correctly

const push = PushNotification.init({
        android: {
          forceShow: true
        },
        ios: {
          alert: 'true',
          badge: true,
          sound: 'true'
        }
      });

cordova info

Ionic:

Ionic CLI : 5.4.16 (/usr/local/lib/node_modules/ionic) Ionic Framework : @ionic/angular 5.1.1 @angular-devkit/build-angular : 0.901.7 @angular-devkit/schematics : 9.1.7 @angular/cli : 9.1.7 @ionic/angular-toolkit : 2.2.0

Cordova:

Cordova CLI : 10.0.0 (cordova-lib@10.1.0) Cordova Platforms : ios 6.2.0 Cordova Plugins : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.2.1, (and 5 other plugins)

Utility:

cordova-res (update available: 0.15.4) : 0.11.0 native-run (update available: 1.5.0) : 0.3.0

System:

ios-deploy : 1.9.2 ios-sim : 8.0.2 NodeJS : v10.19.0 (/usr/local/bin/node) npm : 6.13.4 OS : macOS Catalina Xcode : Xcode 12.0.1 Build version 12A7300

PuffPastry commented 2 years ago

Did you setup the apns key in firebase project? Also, i use the setting

PushNotification.init({
        ios: {
          alert: true,
          badge: true,
          sound: true,
          fcmSandbox: true
        }
};
nosTa1337 commented 2 years ago

Don't know if there is a direct way but I made it work like that (see code below). I have converted the APN key to a firebase token and am using that one to send my requests.

const apnHeader = {
  'Content-Type': 'application/json',
  Authorization: 'key=yourkey',
};

const iosAPNURL = 'https://iid.googleapis.com/iid/v1:batchImport';

export async function apnToFCMToken(apnToken: string) {
  try {
    let sandbox = process.env.NODE_ENV === 'production' ? false : true;
    let body = {
      application: 'your.package.NAME',
      sandbox: sandbox,
      apns_tokens: [apnToken],
    };

    let post = await axios.post(iosAPNURL, body, { headers: apnHeader });

    //iterate over response
    let fcmToken = null;

    if (post.data?.results) {
      let results = post.data.results;
      for (let result of results) {
        if (result.status == 'OK') fcmToken = result.registration_token;
      }
    }

    if (fcmToken) return fcmToken;
    return null;
  } catch (err) {
    logger.error(err);
    return null;
  }
}`
PuffPastry commented 2 years ago

@nosTa1337 Please open a new issue, as you were able to get a registration token and your problem is with topics.