build method
Implementation
Future<NUIPush> build() async{
//Build shared pref
await NUISharedPrefBuilder().build();
// Build NUIWeb for OneSignal
NUIWebBuilder()
.headers(() => {
"Content-Type": "application/json"
})
.module(NUIPush._NUIWEB_ONESIGNAL)
.baseURL(NUIPush._ONE_SIGNAL_REST_URL)
.build();
// Build NUIWeb for iPush
Map<String, String> pushDefaultHeaders = {"Content-Type": "application/json"};
if(_overwriteWebBuilder == null){
NUIWebBuilder()
.module(NUIPush.NUIWEB_MODULE)
.headers(() => pushDefaultHeaders)
.baseURL(_baseUrl)
.build();
}else{
_overwriteWebBuilder!.module(NUIPush.NUIWEB_MODULE);
_overwriteWebBuilder!.baseURL(_baseUrl);
_overwriteWebBuilder!.build();
}
final push = NUIPush._builder(this);
// Run OneSignal with debug logging in debug mode only,
// otherwise debug info e.g, notification's message will be disclosed in release mode.
if (kDebugMode) {
OneSignal.Debug.setLogLevel(OSLogLevel.verbose);
}
OneSignal.initialize(_oneSignalAppId);
OneSignal.User.pushSubscription.optIn();
if (_disableInAppMessage ?? true) {
OneSignal.InAppMessages.paused(true);
}
// Cause some ios/android version crash. Handle notification with permission_handler package instead.
// await OneSignal.Notifications.requestPermission(true);
// OneSignal.Notifications.addForegroundWillDisplayListener((OSNotificationWillDisplayEvent event) {
// print("NUIPush: OneSignal received listener for notification will show in foreground");
// // will be called whenever a notification is received
// if(NUIPush.instance?._notificationReceivedListener != null){
// print("NUIPush: triggering notification received listener");
// NUIPush.instance!._notificationReceivedListener!(NUIPushNotification(osNotification: event.notification));
// }
// else{
// print("NUIPush: notification received listener not configured");
// }
// });
// OneSignal.Notifications.addClickListener((OSNotificationClickEvent event) {
// print("NUIPush: OneSignal received listener for notification opened");
// // will be called whenever a notification is opened/button pressed.
// if(NUIPush.instance?._notificationOpenedListener != null){
// print("NUIPush: triggering notification opened listener");
// NUIPush.instance!._notificationOpenedListener!(NUIPushNotification(osNotification: event.notification));
// }
// else{
// print("NUIPush: notification opened listener not configured");
// }
// });
// Add notification event display
// OneSignal.Notifications.addForegroundWillDisplayListener((event){
// print('NUIPush: NOTIFICATION WILL DISPLAY LISTENER CALLED WITH: ${event.notification.jsonRepresentation()}');
// // /// Display Notification, preventDefault to not display
// // event.preventDefault();
// /// notification.display() to display after preventing default
// event.notification.display();
// });
/// When the application is first initialize, subscription observer will check and return new playerId
OneSignal.User.pushSubscription.addObserver((state) {
// will be called whenever the subscription changes
// (ie. user gets registered with OneSignal and gets a user ID)
print("NUIPush: OneSignal subscription observer received new changes, id=${state.current.id}");
// Always update the persistent when subscription has been changed,
if(!isNullOrEmpty(state.current.id ?? "")){
_updateOSUserIdLocally(push, state.current.id!);
if(NUIPush.instance?._notificationUserIDListener != null){
print("NUIPush: triggering notification user id listener");
NUIPush.instance!._notificationUserIDListener!(state.previous?.id ?? "", state.current.id!);
}
else{
print("NUIPush: notification user id listener not configured");
}
}
});
final deviceToken = OneSignal.User.pushSubscription.id;
if(!isNullOrEmpty(deviceToken ?? "")){
_updateOSUserIdLocally(push, deviceToken!);
if(_notificationUserIDListener != null) {_notificationUserIDListener!("",deviceToken);}
}
return push;
}