-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Firebase/messaging #223
Closed
Closed
Firebase/messaging #223
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a08e183
Added support to integrate firebase messaging module through iterable…
vishal753 964bdf8
Added more fixes
vishal753 1d42e92
Added onMessage listener to listene push notifications
vishal753 de872ac
Added support to receive web push notifications
vishal753 ad50eff
hardcode API_KEY removed, sdk initialize call added to add API_KEY
vishal753 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Iterable's Messaging Integration | ||
|
||
Firebase Messaging Service is a powerful cloud-based messaging platform that enables developers to send notifications and messages to users across various platforms, including web, mobile, and desktop. we are offering integration of Firebase Messaging Service through Iterable's Web SDK, and allowing developers to harness the full potential of Firebase Message in their web applications. | ||
|
||
# Installation | ||
|
||
To install this SDK through NPM: | ||
|
||
``` | ||
$ npm install @iterable/web-sdk | ||
``` | ||
|
||
with yarn: | ||
|
||
``` | ||
$ yarn add @iterable/web-sdk | ||
``` | ||
|
||
or with a CDN: | ||
|
||
Configure Firebase | ||
https://support.iterable.com/hc/en-us/articles/115004760086-Web-Push-Overview-#configuring-firebase | ||
|
||
|
||
Add firebase-messaging-sw.js file to app's public folder | ||
https://github.com/firebase/quickstart-js/blob/master/messaging/firebase-messaging-sw.js | ||
|
||
|
||
# Example | ||
|
||
```ts | ||
import { PMessaging } from '@iterable/web-sdk'; | ||
|
||
function App() { | ||
|
||
React.useEffect(() => { | ||
const init = async () => { | ||
const PMObject = new PMessaging({ | ||
apiKey: "AIzaSyBQ_MAq2O-lTxq7eHGfn_H1_u9j9b0JgZU", | ||
authDomain: "iterable-web-sdk.firebaseapp.com", | ||
projectId: "iterable-web-sdk", | ||
storageBucket: "iterable-web-sdk.appspot.com", | ||
messagingSenderId: "254172754825", | ||
appId: "1:254172754825:web:71536f92e90c2a5969e758", | ||
vapidkey: "BKab2TK0UC8jpQRWHRVXkb58MXGMJE6KidcuUxIUIvPHME3il_JXImADp7_JKdU7ViU0TLOiu4yt_DRaMbugEoc", | ||
}); | ||
|
||
PMObject.setUserId("user1234"); | ||
|
||
PMObject.addEventListener((data: any) => { | ||
console.log("On-Message Triggered", data); | ||
}); | ||
} | ||
|
||
init(); | ||
}, []); | ||
} | ||
``` | ||
|
||
By passing configuration params, initialize PMessaging instance which internally create instace of firebase app and messaging module, | ||
Listene push notifications by adding listener using PMObject.addEventListener(callback). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface IMessageService { | ||
apiKey: string; | ||
authDomain: string; | ||
projectId: string; | ||
storageBucket: string; | ||
messagingSenderId: string; | ||
appId: string; | ||
vapidkey: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { IMessageService } from './types'; | ||
import { baseIterableRequest } from '../request'; | ||
import { IterableResponse } from '../types'; | ||
import { REGISTER_TOKEN } from '../constants'; | ||
import { initializeApp } from 'firebase/app'; | ||
import type { FirebaseApp } from 'firebase/app'; | ||
import { getMessaging, getToken, onMessage } from 'firebase/messaging'; | ||
import type { Messaging } from 'firebase/messaging'; | ||
|
||
export class PMessaging { | ||
private fireAppInstance: FirebaseApp; | ||
private messaging: Messaging; | ||
private listeners: Array<Function>; | ||
private userId: string; | ||
|
||
constructor(config: IMessageService) { | ||
this.fireAppInstance = initializeApp(config); | ||
this.messaging = getMessaging(this.fireAppInstance); | ||
this.requestPermission(config.vapidkey); | ||
this.listeners = []; | ||
this.userId = ""; | ||
|
||
onMessage(this.messaging, (payload: any) => { | ||
this.triggerEvent(payload); | ||
}); | ||
} | ||
|
||
public addEventListener(callback: Function) { | ||
this.listeners.push(callback); | ||
} | ||
|
||
public setUserId(userId: string) { | ||
this.userId = userId; | ||
} | ||
|
||
private triggerEvent(eventData: any) { | ||
if (this.listeners.length) { | ||
this.listeners.forEach((callback: Function) => callback(eventData)); | ||
} | ||
} | ||
|
||
private async requestPermission (vapidKey: string) { | ||
let me = this; | ||
Notification.requestPermission().then(async (permission) => { | ||
if (permission === 'granted') { | ||
const currentToken = await getToken(me.messaging, { vapidKey: vapidKey }); | ||
if (currentToken) { | ||
await me.registerBrowserToken(currentToken); | ||
} else { | ||
console.warn('Failed to generate the app registration token.'); | ||
} | ||
} else { | ||
console.warn('User Permission Denied.'); | ||
} | ||
}); | ||
}; | ||
|
||
private async registerBrowserToken (token: string) { | ||
return baseIterableRequest<IterableResponse>({ | ||
method: 'POST', | ||
url: REGISTER_TOKEN, | ||
headers: { | ||
"Api-Key": "1d925bbf54f34bcb92a15da14bfb5d1d" | ||
}, | ||
data: { | ||
userId: this.userId, | ||
browserToken: token | ||
} | ||
}); | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to remove the hardcoded api key here from the commit history and make this use the environment variable in
.env.example
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we added the "initialize(process.env.API_KEY)" call on the example app, to add "API_KEY" for API calls, that's why we removed the static header