activitree / meteor-push

Meteor Push Notifications for Cordova and Web/PWA with Firebase (FCM).
MIT License
27 stars 19 forks source link

Can't get clients registered (_push_app_tokens not populated) #38

Closed idac89 closed 3 years ago

idac89 commented 3 years ago

Hi all! I can't get the _push_app_tokens populated with Android or Web client. I've set both server and client startup js files as in the example app. The debug write the configuration then says: Firebase Admin for Android Messaging configured Push: Send worker started, using interval: 3000

But If I try to send a notification with Push.send i get: Push, GUIDE: The "Push.appCollection" might be empty. No clients have registered on the server yet...

What can I do to give it a try forcing a client registration and check when it goes into some errors?

Thanks in advance

paulincai commented 3 years ago

Ok, let's check on the web and then Android.

  1. Are you in Production over an SSL connection?
  2. Do you have the firebase-messaging-sw.js in your public folder?

The web worker should contain this script:

importScripts('https://www.gstatic.com/firebasejs/8.2.3/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/8.2.3/firebase-messaging.js')

if (firebase && firebase.messaging && firebase.messaging.isSupported()) {
  firebase.initializeApp({ projectId: 'xxxxxx', apiKey: 'xxxxxxx', appId: 'x:xxxxxxxxxxxxxxxxx', messagingSenderId: 'xxxxxxxxx' })
  const messaging = firebase.messaging()
}

self.addEventListener('notificationclick', event => {
  if (event.action) {
    clients.openWindow(event.action)
  }
  event.notification.close()
})

If you use browser policies in you project, just make sure those import from gstatic.com are not being blocked by your policy.

idac89 commented 3 years ago

Thanks for your answer! 1- I'm testing notification running Meteor on localhost:3000 2- The firebase-messaging-sw.js was in public folder but was a little bit different from the one that you've posted. I've edited it now. 3- There are no policy blocking gstatic.com domain.

paulincai commented 3 years ago
  1. In development I don't see any interaction in the browser for allowing to receive notifications. This is why I was asking whether you were in production over SSL. I think you should try that first. You should see this option in the meniu of your browser when you click on that SSL lock right before the url in the address bar: Screen Shot 2021-07-01 at 4 05 59 PM
idac89 commented 3 years ago

Ok, thanks! I will try the app on a test environment over a SSL and let you know!

idac89 commented 3 years ago

I've tried over SSL but _push_app_tokens table is not yet populated. Cattura

idac89 commented 3 years ago

I've tried many ways without any success. Any hint? Thankyou

paulincai commented 3 years ago

Did it work for you on IOS? I see you mentioned it didn't work on Android and Web. Also do you import this:

import { webPushSubscribe } from 'meteor/activitree:push'

and do the subscription anytime after your website is loaded?

idac89 commented 3 years ago

Thanks For reply! Unfortunately nothing seems ti work on iPhone too.

Yes! For testing purposes I've tried putting the call to webPushSubscribe() in a method called via Meteor.call on a button click but I get: Expetion while invoking method "testsubscribe" TypeError: webPushSubscribe Is not a function.

Obviously i've import { webPushSubscribe } from 'meteor/activitree:push' on top of file.

paulincai commented 3 years ago

Hi, please read this thread to better understand Meteor.call(): https://stackoverflow.com/questions/39229966/meteor-call-method-meteor-js

To explain it further, for this case, a Meteor package can be configured to add code to the client bundle, to the server bundle or to both. Generating a Push token is exclusively a client feature and you cannot call this function using a Meteor Method. You just need to execute the function itself. Example:

In your App.js (with React):

import React, { useEffect, useState } from 'react'
import { webPushSubscribe } from 'meteor/activitree:push'

const App = props => {

      // ..........
       useEffect(() => {
           // wait a number of seconds before you ask a user to accept receiving notifications
           setTimeout(() => {
                  webPushSubscribe()
           }, 5000) // 5 seconds
       }, [])
      // .........
}
idac89 commented 3 years ago

Ouch! I've understood! Now I finally got web client token registered on db but when I try to send a push notification i get: Push: Sent message "test" to 0 iOS apps | 0 Android apps | 1 web apps Push, GUIDE: The "Push.appCOllection" - No APN clients have registered on the server yet... Succesfully sent message: projects/meteor-push-35e5d/messages/xxxxxxxxxxx

But i don't see any push notification

idac89 commented 3 years ago

Update: Randomly i see the push notification.

I've 2 buttons For testing purposes:

paulincai commented 3 years ago

Hi @idac89, did you manage to implement Push and consistently get tokens?

idac89 commented 3 years ago

Hi! Yes, when i click on the button that call webPushSubscribe, It Just get token and write It on db. And when I try to send webpush notification It works randomly.

paulincai commented 3 years ago

" It Just get token and write It on db." Not sure I understand right, do you expect to do something else, or extra? It sends randomly or receives randomly in the browser. The package has a debug flag which when turned on it you get to see everything in the console. With this in mind, did you turn debug ON and do you see the sending "receipt" in the server console? Is the sending successful or does it show sent to 0 or ... I will close this for the time being. I would consider that if the package is able to send once, there is nothing wrong with it. If however you find an issue, or you want to improve upon the existing package or need an extra feature, please feel free to open a PR and I will make sure to merge it for you in due time.