I'm using Twilio programmable chat and wanted to send push notification to mobile app(iOS, Android) and web app as well. I followed the steps given in Twilio Still, I'm not getting notifications in web and mobile apps as well. Following is the code I implemented.
<script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-messaging.js"></script>
<script src="firebase-messaging-sw.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "******************",
authDomain: "*****.firebaseapp.com",
databaseURL: "https://******.firebaseio.com",
projectId: "******",
storageBucket: "******.appspot.com",
messagingSenderId: "***************"
};
firebase.initializeApp(config);
</script>
During user login I'm doing the following
/* Generating token for Twilio chat */
$scope.URL = Path + "/twilio/chat/generateToken";
var data = {"identity":localStorage.userId,"deviceId":guid(),"pushCredentialSid":"**********"}
RestAPIService.post($http,data,$scope,$scope.URL, function ( responsesData ) {
if(responsesData.data.status == "success"){
var twilioToken = responsesData.data.token;
Twilio.Chat.Client.create(twilioToken).then(function(twilioClient) {
twilioChatClient = twilioClient;
// Twilio notification
firebase.initializeApp(config);
if (firebase && firebase.messaging()) {
// requesting permission to use push notifications
firebase.messaging().requestPermission().then(function() {
console.log ("Notification permission granted.");
// getting FCM token
firebase.messaging().getToken().then(function(fcmToken) {
console.log ("token is:" + fcmToken);
// continue with Step 7 here
// passing FCM token to the `chatClientInstance` to register for push notifications
twilioChatClient.setPushRegistrationId('fcm', fcmToken);
// registering event listener on new message from firebase to pass it to the Chat SDK for parsing
/* firebase.messaging().onMessage(function(payload){
twilioChatClient.handlePushNotification(payload);
}); */
}).catch(function(err) {
// can't get token
console.log(err);
});
}).catch(function(err){
// can't request permission or permission hasn't been granted to the web app by the user
console.log(err);
});
} else {
// no Firebase library imported or Firebase library wasn't correctly initialized
}
/* Twilio notification */
});
}
});
I'm not sure, how to proceed further, and didnt know whether I missed anything. If someone one who has implemented push notification from web app to mobile apps please guide me to proceed further.