0
votes

I am trying to implement push notifications in my app developed with Ionic 3. I am following this tutorial : https://medium.com/@ankushaggarwal/generate-apns-certificate-for-ios-push-notifications-85e4a917d522

Everything went well for Android, on which I get notifications.

But, on iOS, when I send a push notification, I get absolutely nothing on iOS.

I did the following :

  • get an APN push certificate
  • uploaded it in my Ionic account
  • switched "Push notifications" on under the "capabilities" tab in Xcode
  • also switched on "Background modes / Remote notifications" under the same tab

My code is the same as the one in the tutorial above :

initPushNotifications() {
    if (!this.platform.is("cordova")) {
        console.warn("Push notifications not initialized. Cordova is not available - Run in physical device");
        return;
    }

    const options: PushOptions = {
        android: {
            senderID: "784098698049"
        },
        ios: {
            alert: "true",
            badge: false,
            sound: "true"
        },
        windows: {}
    };

    const pushObject: PushObject = this.push.init(options);

    pushObject
    .on("registration")
    .subscribe((data: any) => {
        console.log("device token -> " + data.registrationId);

        this.userService.edit({
            ionicToken: data.registrationId
        });
    });

    pushObject.on("notification").subscribe((data: any) => {
        console.log("message -> " + data.message);
        //if user using app and push notification comes
        if (data.additionalData.foreground) {
            // if application open, show popup
            let confirmAlert = this.alertCtrl.create({
                title: "New Notification",
                message: data.message,
                buttons: [{
                    text: 'Ignore',
                    role: 'cancel'
                }, {
                    text: 'View',
                    handler: () => {
                        //TODO: Your logic here
                    }
                }]
            });
            confirmAlert.present();
        } else {
            //if user NOT using app and push notification comes
            //TODO: Your logic on click of push notification directly
            console.log('Push notification clicked');
        }
    });

    pushObject.on('error').subscribe(error => console.error('Error with Push plugin' + error));
}

I also tried following the official documentation at https://docs.ionic.io/services/push/, which doesn't use the same modules. Still, the result is the same : it works on Android but not on iOS.

I don't know if I can find some logs somewhere, to figure out if there is an error. If somebody has informations about this, please share.

Does anybody had any success on implementing push notifications with Ionic ?

Thanks.

1

1 Answers

0
votes

You need to run your app either through Apple's testflight or App store. In my experience, push notifications don't work if you just run your app locally on you phone.