1

Hi I am using ionic 2 and require to send push notifications in a pubsub format(user posts content to a topic and subscriber to the on the other app gets a push notification ), have already implemented the cordova-plugin-fcm from https://github.com/fechanique/cordova-plugin-fcm

on the documentation page its mentioned to send push notification via this code

//POST: https://fcm.googleapis.com/fcm/send
//HEADER: Content-Type: application/json
//HEADER: Authorization: key=AIzaSy*******************
{
  "notification":{
    "title":"Notification title",  //Any value
    "body":"Notification body",  //Any value
    "sound":"default", //If you want notification sound
    "click_action":"FCM_PLUGIN_ACTIVITY",  //Must be present for Android
    "icon":"fcm_push_icon"  //White icon Android resource
  },
  "data":{
    "param1":"value1",  //Any data to be retrieved in the notification callback
    "param2":"value2"
  },
    "to":"/topics/topicExample", //Topic or single device
    "priority":"high", //If not set, notification won't be delivered on completely closed iOS app
    "restricted_package_name":"" //Optional. Set for application filtering
}

Question : where to add this code in ionic 2 app, is it to be added in .ts file to the function once it finishes execution or where ?

the function code to post content is :-

code from : job.ts

self.dataService.submitJob(newJob, uid, category)
  .then(function (snapshot) {
    loader.dismiss()
      .then(() => {
        self.viewCtrl.dismiss({
          newjob: newJob,
          uid: uid,
          category: category
        });
      }).then(() => {
        let toast = self.toastCtrl.create({
          message: 'Job Posted Succesfully',
          duration: 3000,
          position: 'top'
        });
        toast.present();
      });
  }, function (error) {
    console.error(error);
    loader.dismiss();
  });

what i want to know is do i have to call the code to send push notification here ?

dhruv
  • 151
  • 2
  • 3
  • 14
  • Ah I see you created a new one, good. I'll submit this as a comment, **(1)** The JSON structure mentioned above will have to be constructed by your backend. **(2)** to receive a notification (is I believe what you asked) you have the `push.on('notification', (data) => { ...});` function. **(3)** to call the code to push a notification, call `this.http.post(serverUrl, {data : data, device_id: deviceToken});` where `data` is f.e. your message and the `deviceToken` has been retrieved from the registration. – Ivar Reukers Dec 07 '16 at 18:15
  • yaa thanks, backend part i still dont understand , i dont have any backend at all, isnt firebase itself supposed to be a baas – dhruv Dec 07 '16 at 18:32
  • Not sure, probably yes, which makes me wonder if firebase itself has a way of sending the notifications instead of via a custom backend – Ivar Reukers Dec 07 '16 at 18:33
  • i am able to send notifications from the console, so by any logic they do have a server to handle requests, see this discussion : http://stackoverflow.com/questions/37311188/migration-from-gcm-to-fcm-needed/37314483#37314483 – dhruv Dec 07 '16 at 18:35
  • @Ivaro18 i am having issues with http post request are you using angular http post or ionic native http post ? can you have a look at my question : http://stackoverflow.com/questions/41033895/http-post-to-fcm-server-not-working – dhruv Dec 08 '16 at 07:22
  • I was actually laying in bed yesterday thinking about this question, and indeed your solution came to my mind, sending the post from angular instead of a custom backend hahaha – Ivar Reukers Dec 08 '16 at 07:52
  • hehe , let me try the angular http code and report you back – dhruv Dec 08 '16 at 07:53

0 Answers0