ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.31k stars 962 forks source link

Push Notifications #380

Closed JStumpp closed 5 years ago

JStumpp commented 6 years ago

Push notification is very often required in apps. Any plans on implementing this? The optimal solution would be to have a unified api for PWA, iOS, Android and Electron. The Phonegap plugin push is a good reference: https://github.com/phonegap/phonegap-plugin-push

I think the best idea would be to use the Firebase Cloud Messaging for all platforms which simplifies the code to write on the server-side.

mlynch commented 6 years ago

Yea we're thinking about how to do that right now. As for FCM, I have some hesitations. For one, I don't want to exclude apps that are using APNS directly, but maybe it does make it a lot simpler?

mlynch commented 6 years ago

Going to prioritize this because it's probably going to be a show stopper for anyone using Capacitor f it doesn't have easy Push support out of the box, and the existing PhoneGap one doesn't play well with Capacitor yet

edy-ap commented 6 years ago

Onesignal is a best option

jcesarmobile commented 6 years ago

why?

edy-ap commented 6 years ago

https://mobilejazz.com/blog/push-notifications-firebase-vs-onesignal/

Only for push notifications is better

mlynch commented 6 years ago

Since there are so many 3rd party options out there, our push support should try to support the native SDKs and services provided by Apple and Google. We would love to see Onesignal as a plugin option but we're not going to bake it into the official plugin at this time

edy-ap commented 6 years ago

but could you support it as a cordova plugin?

jcesarmobile commented 6 years ago

It doesn't work at the moment, I've fixed several issues it had, but more issues keep coming, so I wouldn't put my hopes on it.

edy-ap commented 6 years ago

I will continue testing and try to solve some errors, by the way I see that Capacitor 1.0.0 Alpha, yeaaa!

speedfl commented 6 years ago

Most of the people who are using ionic/cordova app are using phonegap push plugin To start I tkink it could be a good idea to take this one even if it uses fcm. And next add apns support.

jcesarmobile commented 6 years ago

phonegap push plugin still uses APNs as default, FCM only if you provide the FCM configuration .plist file.

iranicus commented 6 years ago

I'm currently using phonegap push plugin for an app I built with ionic for android and iOS, I use FCM for this (having a GoogleServices.json for Android and .plist for iOS), having just recently heard of capacitor I've been trying it out but currently blocked at making it work with the phonegap-push-plugin v2.0.0 and v2.1.2 (build issues for android). Hopefully if it isn't currently supported then it will be supported soon so I can continue working with it. 👍

adrm commented 6 years ago

Hi, this is a showstopper for us. If there is no support for push notifications yet we will have to use cordova meanwhile. I will be glad to try some experimental plugin for capacitor if it's available. Is there an ETA for this feature?

Thanks.

jjviscomi commented 5 years ago

We are planning on leveraging Capacitor to wrap our PWA and are specifically targeting iOS. We require Push Notification functionality. Our engineering team is happy to pitch in on the development of it. We would like to know the status of this internally, obviously we don't want to step on any toes or reinvent the wheel. @mlynch I noticed that you marked it for Beta 2 what is the time line on this, what do you need? We can dedicate 1 or 2 engineers for a few weeks to punch this out ... send me a message.

ghost commented 5 years ago

I have used the cordova-fcm-plugin - it's far more easier, i just modified. (https://github.com/fssrepository/cordova-plugin-fcm-modified) - i had no test device, but it's very lightweight. (just a pure clone from what the firebase android-start project has)

If we use fcm or apns, then it can be done in some hours, even if you don't know Swift. If you would like to create from scratch it would take some time.

To me is not needed, as my app is using mqtt at the end. Needs a little bit more than what the notification can do.

Unfortunately to integrate fcm to the base product is not a good idea, as it's very heavy.

To be honest the cordova world is very scattered, and i think we need to group plugins in one. For example some common what every app contains. (auth / push etc. in one - it does not take too much space)

A-Industry commented 5 years ago

I am currently using the PushNotification Plugin of Capacitator beta.7. This works great on Android. On iOS I see this in the logs:

⚡️ To Native -> PushNotifications register 62318285 ⚡️ TO JS {}

So registering the push functionality works. But PushNotifications.addListener('registration', token => { never receives a token on iOS.

A-Industry commented 5 years ago

registrationError is never called as well

jcesarmobile commented 5 years ago

Did you enable push capabilities in Xcode?

A-Industry commented 5 years ago

Yes I did.

screen shot 2018-08-29 at 08 57 14
jcesarmobile commented 5 years ago

Can you create a new issue and provide the information there? Like code you are using or a sample app if possible

A-Industry commented 5 years ago

@jcesarmobile I send you a message on Slack (getcapacitor) about this

mval1002 commented 5 years ago

I ran into a very similar issue - I eventually learned that if there is no connection to APNS to register the token, neither the success nor the error function is called. I simply restarted my phone, and everything worked from there. This took me a full week to figure out, so hopefully this helps someone else (even if it ends up not being your issue, @A-Industry ).

To this day I don't know how Apple could be that careless, or why a connection couldn't be established since everything else was working fine!

ionut-movila commented 5 years ago

Is there any documentation for how to use capacitor PushNotifications plugin?

Asking because I tried to use phonegap-plugin-push, but it has grade conflicts for package 'com.google.firebase:firebase-messaging' with what is already available in 'capacitor-android'.

ionut-movila commented 5 years ago

I am currently using the PushNotification Plugin of Capacitator beta.7. This works great on Android. On iOS I see this in the logs:

⚡️ To Native -> PushNotifications register 62318285 ⚡️ TO JS {}

So registering the push functionality works. But PushNotifications.addListener('registration', token => { never receives a token on iOS.

Can you tell me how you did initialize Firebase on android?

I'm getting this error regarding Firebase init: Default FirebaseApp is not initialized in this process ... Make sure to call FirebaseApp.initializeApp(Context) first.

And I do have google-services.json file in android/app folder.

Do we need another plugin just to initilize Firebase, or this should happen out of the box with Capacitor?

A-Industry commented 5 years ago

@ionut-movila To resolve the conflicts I had to add this to app/build.gradle:

configurations.all { resolutionStrategy.force 'com.google.firebase:firebase-iid:16.2.0' }

sbozzie commented 5 years ago

Linked to issue: #813 - we're struggling to get Firebase FCM notifications working.

ionut-movila commented 5 years ago

Got it working on Android. I have to manually add Firebase dependency and start Firebase app.

Now the problem is with iOS, because it does support push notifications but only native kind, not with Firebase. And since our backend delivers to both platforms via Firebase, this is not good at all.

I have tried with two other known plugins: phonegap-plugin-push and cordova-plugin-firebase but both gave me conflicts either on iOS (duplicate frameworks when using with Fabric due to ios lib capacitor-cordova-ios-plugins) and Android (gradle conflict for phonegap-plugin-push).

Looks like a no way to make this work, both platforms using Firebase :-|

solojuve1897 commented 5 years ago

Like @ionut-movila I get the same issue.

I'm getting this error regarding Firebase init: Default FirebaseApp is not initialized in this process ... Make sure to call FirebaseApp.initializeApp(Context) first.

And I do have google-services.json file in android/app folder.

I also have google-services.json file in the android/app folder.

What's missing?

solojuve1897 commented 5 years ago

@mlynch @jcesarmobile, any suggestions?

jcesarmobile commented 5 years ago

I have created a sample app

Didn't have to add anything else to make it work on Android, just putting the google-services.json inside android/app/ On iOS you have to go to Capabilities and set Push Notifications switch to On.

The sample app, basically calls register to start the push, and add a listener for registration and another for pushNotificationReceived. Right now just logs the token on registration and the stringified notification on pushNotificationReceived

jcesarmobile commented 5 years ago

I just cloned my own sample app in a different computer and got that error about not being initialized.

The problem was the google-services.json wasn't added on the sample app, so I downloaded it and put it in android/app and sync gradle and ran again and it worked fine.

If having problems read the build log on Android Studio, if it doesn't read the google-services.json a warning will appear there.

solojuve1897 commented 5 years ago

@jcesarmobile It worked after I removed the android directory: "rm -rf android/" and added it again "npx cap add android".

Though I see the following warning in Android Studio: "Warning: The app gradle file must have a dependency on com.google.firebase:firebase-core for Firebase services to work as intended."

It works as expected though, so it doesn't matter.

I will hopefully implement everything regarding push notifications on saturday and will get back if I stumble upon something strange during the process.

jcesarmobile commented 5 years ago

Yeah, looks like google doesn’t like us only using the messaging part and shows that warning so we include core. Probably what won’t work is analytics, but push seems to work just fine

jcesarmobile commented 5 years ago

Going to close this as the push notification plugin was added a few versions ago and documented on last release.

If you find bugs while using it or want more features added to push, report new issues

leshik commented 5 years ago

@jcesarmobile I was able to make it working on iOS thanks to your sample app! But on Android, notifications aren't shown for some reason. I can see they are arriving in console (so it's not google-services.json issue), they just aren't displaying. What might be the reason?

leshik commented 5 years ago

Oh, nevermind – I was using the wrong payload (seems something changed since GCM).

oliverandersencox commented 5 years ago

@jcesarmobile is it possible to send FCM notifications to the capacitor Push Notifications plugin?

rqton commented 5 years ago

I have created a sample app

Didn't have to add anything else to make it work on Android, just putting the google-services.json inside android/app/ On iOS you have to go to Capabilities and set Push Notifications switch to On.

The sample app, basically calls register to start the push, and add a listener for registration and another for pushNotificationReceived. Right now just logs the token on registration and the stringified notification on pushNotificationReceived

I just cloned my own sample app in a different computer and got that error about not being initialized.

The problem was the google-services.json wasn't added on the sample app, so I downloaded it and put it in android/app and sync gradle and ran again and it worked fine.

If having problems read the build log on Android Studio, if it doesn't read the google-services.json a warning will appear there.

YOU MAKE MY DAY @jcesarmobile all love

oliverandersencox commented 5 years ago

@jcesarmobile forgive my ignorance, but on android how are you sending these notifications?

Can I send them from the firebase cloud messaging section and the plugin will work with that?

I have it working on IOS but not android

jcesarmobile commented 5 years ago

You can send them from FCM console, but FCM console sends the push with notification payload. That means, it only triggers pushNotificationReceived if the app in the foreground, when in background the OS will show the notification on the notification bar. Then if you click them, the app will open, and if they also had a data part on the push payload, that will be passed to pushNotificationActionPerformed.

For quick testing, I use is postman, here you can read how. It has examples for notifcation+data and for only data payloads. https://medium.com/android-school/test-fcm-notification-with-postman-f91ba08aacc3

When sending just data payload, pushNotificationReceived is always called, even if the app is closed or in background, but not displayed to the user.

henryavila commented 4 years ago

Hi @jcesarmobile, can you confirm if the Push Notification works with OneSignal? I plan to migrate my app to Cordova, but OneSignal is a MustHave for me, beacause my app use massively some OneSignal feature.

herbola commented 4 years ago

I am currently using the PushNotification Plugin of Capacitator beta.7. This works great on Android. On iOS I see this in the logs: ⚡️ To Native -> PushNotifications register 62318285 ⚡️ TO JS {} So registering the push functionality works. But PushNotifications.addListener('registration', token => { never receives a token on iOS.

Can you tell me how you did initialize Firebase on android?

I'm getting this error regarding Firebase init: Default FirebaseApp is not initialized in this process ... Make sure to call FirebaseApp.initializeApp(Context) first.

And I do have google-services.json file in android/app folder.

Do we need another plugin just to initilize Firebase, or this should happen out of the box with Capacitor?

did you solve this?

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.