scriptPilot / app-framework

Applications for any device with HTML, CSS and JavaScript - free and open source!
MIT License
654 stars 143 forks source link

Support Firebase Cloud Messaging Notifications #641

Open miner150000 opened 6 years ago

miner150000 commented 6 years ago

Notifications are one of the most important things when doing mobile apps as you can inform the client about any updates on the app, i was trying to find any way that i could easily add this functionality to the framework but it seems that there is no built-in way to seamlessly do it.

i thought it would be easy as the framework is already using Firebase for many other functionalities as storage, deployment, even authentication, maybe i haven't found it but i would definitely try to add this functionality, and when i do if i succeed i'll try to make a pull request for you, thank you very much for this framework, let's hope it grows as the others.

Todo-

-add FCM plugin as default -add a way to directly put google messaging configuration files on a folder for the build to register them and put them on the correct path -add a small tutorial on how to create, register, get token, send and manage notifications and add it to the documentation of the framework.

miner150000 commented 6 years ago

Done.

i found some problems for generic implementations that could be improved so i didn't do it as default -issue (not resolved it just need some conditionals): the fcm plugin needs to have the configuration files, if it doesn't it wouldn't compile (if you get an error from debuggoogleservices then this is the reason) -issue (solved): these files have to reside inside cordova's project folder, as this folder is generated in each build then it wouldn't be as simple as copy the files directly to de cordova folder, it has to be part of the building process. -issue(not solved but manage to workaround): strangely when building the code seems to test if there is a playstorid defined and it print it to the android manifest, but when installing this plugin it just stays with the default com.app-framework.dev-build so the workaround was just to change the default name to default as mine but i didn't found the reason for this behavior.

But Notifications are now working:

steps:

1: add plugin to the config.json plugin list: "cordova-plugin-fcm"

2: add the following methods to copy the files from the base app folder (where the coder would put the downloaded files) to the base created cordova project

let updateIOSNotificationFile = function (callback){
  alert ('Copying Firebase Notification Files')
    fs.copy(abs(env.app, 'GoogleService-Info.plist'), abs(binDir, 'GoogleService-Info.plist'), function (err) {
      if(!err){
          alert('IOS Notification File Copied')
      }
      else{
        alert('IOS Notification File Not Found')
      }
      callback()
    })
}
let updateAndroidNotificationFile = function (callback){
    alert ('Copying Firebase Notification Files')
    fs.copy(abs(env.app, 'google-services.json'), abs(binDir, 'google-services.json'), function (err) {
        if(!err){
            alert('Android Notification File Copied')
        }
        else{
            alert('Android Notification File Not Found')
        }
        callback()
    })
}

also call them respectively

deployDevRules(function () {
  cmd(__dirname, 'node cache-version --version ' + env.arg.version, function () {
    resetCordovaFolder(function () {
      createCordovaProject(function () {
        updateCordovaConfig(function () {
          //MODIFIED
          updateIOSNotificationFile(function(){
            updateAndroidNotificationFile(function(){
           //MODIFIED
                updateWwwFolder(function () {
...

3: just run the build as always and configure the fcm as usual

pd: if you encounter an error mentioning com.app-framework.dev-build then change line 45 to your app's ID

i hope this helps someone

GeorgeGhiottone commented 6 years ago

Thanks for your solution! I have a problem with this, i have tried your solution but it works only the first time, after that this error appear: Default FirebaseApp is not initialized in this process yy.xxxxxx. Make sure to call FirebaseApp.initializeApp(Context) first. And no notification will be displayed. You have any hints? I realy need the notifications :( pls help me!

Thank you very much