Skip to content

Instantly share code, notes, and snippets.

@brentonhouse
Forked from hansemannn/push-manager.js
Created November 28, 2019 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentonhouse/a4a20839cc31fc7916d77adc61140730 to your computer and use it in GitHub Desktop.
Save brentonhouse/a4a20839cc31fc7916d77adc61140730 to your computer and use it in GitHub Desktop.
Handle iOS push notifications in Appcelerator Titanium
import FirebaseMessaging from 'firebase.cloudmessaging';
export default class PushManager {
listenForPushNotifications() {
// Called when the Firebase token is ready
FirebaseMessaging.addEventListener('didRefreshRegistrationToken', event => {
const fcmToken = event.fcmToken;
// Update push token here …
});
}
register() {
Ti.API.debug('Registering for push notifications ..');
// Listen to the notification settings event
Ti.App.iOS.addEventListener('usernotificationsettings', function eventUserNotificationSettings() {
// Remove the event again to prevent duplicate calls through the Firebase API
Ti.App.iOS.removeEventListener('usernotificationsettings', eventUserNotificationSettings);
// Register for push notifications (Ti)
Ti.Network.registerForPushNotifications({
success: () => {
// Set the token on successfull registration …
},
error: event => {
// Handle error …
},
callback: event => {
Ti.API.debug('Did receive iOS push!');
// Handle push message …
}
});
});
// Register for the notification settings event
Ti.App.iOS.registerUserNotificationSettings({
types: [
Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
]
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment