简体   繁体   中英

What is the best way to send notification user to user ionic 3 / firebase

i am working on a project with ionic 3 and firebase. and i use firebase auth & database.

what i mean by user to user notification is like "x added you as a friend" or "a new post from x" etc.

so when i click the add button, app will send a notification to target uid's device. simple as always.

my question is what is the best and easiest way to do this in ionic & firebase for both ios and android ?

i researched that on the net but there are tons of different ways to do that depend on different platforms i couldnt find specific solution

You can do that with FCM . Add FCM in your project. Now to send notification from user to user/users you need the FCM token/tokens of the another user/users. Then just call this API.

let notifcationObj: any = {
    "notification": {
      "title": "YOUR_TITLE",
      "body" : "x added you as a friend :)",
      "sound": "default",
      "click_action": "FCM_PLUGIN_ACTIVITY"
    },
    "data": {
      "match": "created",
      "matchId": "value2",
      "image": "www/assets/img/logo-gray.png"
    },
    "to": TOKEN/TOKENS(Another user's FCM token or token array),
    "priority": "high",
    "restricted_package_name": ""
  };

  let headers = new Headers({
    'Content-Type': 'application/json',
    'Authorization': 'key=YOUR_FCM_KEY'
  });

  let options = new RequestOptions({ headers: headers });

  let url = "https://fcm.googleapis.com/fcm/send";

  this.http.post(url, notifcationObj, options)
    .subscribe((data) => {
      console.log('notification data -> ', data);
    }, (error) => {
      console.log('notification error -> ', error);
    });

This is best and easiest way to send notification from users end. Let me know if any other help needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM