skip to Main Content

Please ignore the comments I edited my previous questions as I want to start bounty on it

I have an angular project for kindy portal which is where teacher post kids/students post of what they are doing during the day and when a new article is published for a child I want to send a push notification to that specific parent on ios device.

Currently some previous employee used some 3rd party stuff or some technology to create the website to application (from what I know they just wrote a line of code to reference the same code for website to the app) so people will think its an application but its loading stuff from the web. And I don’t have details of it or where the code is for it so I am trying to figure out if its possible to do without the need of APN so i can continue working on it or if its not possible to do without APN then I can’t as I don’t have access to that source code of how they made it to app.

So my question is: if I want to use firebase to send the notification on ios device to specific user is firebase enough or smart enough to do it by its own or do I need APN to be able to get the user token from their ios device and use that to send the notification?

3

Answers


  1. To send push notifications to iOS devices using Firebase Cloud Messaging (FCM), you will need to use the Apple Push Notification Service (APNs) in conjunction with Firebase. FCM acts as a middleman between your server and various push notification services, including APNs for iOS devices. Here’s how the process works:

    1. Get User’s Device Token
    2. Register Device Tokens
    3. Send Notification to FCM
    4. FCM Relays to APNs
    Login or Signup to reply.
  2. Certainly! Here’s a step-by-step example of how to send a push notification to a specific iOS device using Firebase Cloud Messaging (FCM) and APNs in an Angular project:

    1. Set up Firebase Project:

    • Create a Firebase project and set up Firebase Cloud Messaging. Note your Firebase project’s API key and Sender ID.

    2. Add Firebase to your Angular project:

    • Install the Firebase JavaScript SDK:

      npm install firebase
      
    • Initialize Firebase in your Angular project. You typically do this in your app.module.ts:

      import { BrowserModule } from '@angular/platform-browser';
      import { NgModule } from '@angular/core';
      import { AngularFireModule } from '@angular/fire';
      
      const firebaseConfig = {
        apiKey: 'YOUR_API_KEY',
        authDomain: 'YOUR_AUTH_DOMAIN',
        projectId: 'YOUR_PROJECT_ID',
        storageBucket: 'YOUR_STORAGE_BUCKET',
        messagingSenderId: 'YOUR_MESSAGING_SENDER_ID',
        appId: 'YOUR_APP_ID'
      };
      
      @NgModule({
        imports: [
          BrowserModule,
          AngularFireModule.initializeApp(firebaseConfig)
        ],
        declarations: [/* ... */],
        bootstrap: [/* ... */]
      })
      export class AppModule { }
      

    3. Obtain Device Token:

    • In your Angular app, when a user logs in or installs the app, you need to obtain the device token and send it to your server.

    • You can use the Firebase JavaScript SDK to obtain the device token. Here’s an example:

      import { Injectable } from '@angular/core';
      import { AngularFireMessaging } from '@angular/fire/messaging';
      
      @Injectable()
      export class MessagingService {
      
        constructor(private afMessaging: AngularFireMessaging) {
          this.afMessaging.requestToken.subscribe(
            (token) => {
              // Send this token to your server for later use.
              console.log('Device token:', token);
            },
            (error) => {
              console.error('Unable to get permission to notify.', error);
            }
          );
        }
      
        // Other methods for managing tokens and subscriptions.
      }
      

    4. Send a Notification:

    • On your server (typically using a backend server like Node.js), when you want to send a notification to a specific user, you would send a request to FCM with the user’s device token.

    • Here’s a simplified example of how you might send a notification using the firebase-admin Node.js SDK:

      const admin = require('firebase-admin');
      const serviceAccount = require('path/to/serviceAccountKey.json');
      
      admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: 'https://YOUR_PROJECT_ID.firebaseio.com'
      });
      
      const message = {
        data: {
          title: 'Your Notification Title',
          body: 'Your Notification Body'
        },
        token: 'USER_DEVICE_TOKEN'
      };
      
      admin.messaging().send(message)
        .then((response) => {
          console.log('Successfully sent message:', response);
        })
        .catch((error) => {
          console.error('Error sending message:', error);
        });
      

    In this example, replace 'USER_DEVICE_TOKEN' with the actual device token of the target user. The server sends a request to FCM, which then relays the notification to the target device via APNs.

    This is a simplified example, and you may need to implement additional features like managing multiple tokens for a user or handling message payloads according to your requirements.

    Login or Signup to reply.
  3. As a way for Apple to regulate push notifications only Apple can send it. So yeah you MUST have APNS.

    Reasons for that are:

    • Bad app developers would just bombard users with notifications. APNs can drop high frequency messaging to maintain a quality use experience.
    • Apple has a better way to coalesce notifications. Especially silent/background notifications. This has power saving benefits.
    • Apple can give special entitlement for emergency apps. Example certain notifications can bypass the app’s notifications settings if you have the ‘critical alert’ entitlement.
    • By Apple governing the whole payload, payloads then have a standard for sound, localization, summary, subtitle, threading, coalescing, etc. If there wasn’t for Apple then each company would have had their own standard. Imagine some sending in json, others in xml format and and all with different keywords.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search