Created
October 15, 2019 15:28
-
-
Save hansemannn/f69e939563016bc56b92e6971284cbcf to your computer and use it in GitHub Desktop.
Handle iOS push notifications in Appcelerator Titanium
This file contains hidden or 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
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