registerPushDeviceForUser method

  1. @override
Future<DataResult> registerPushDeviceForUser({
  1. required String deviceToken,
})
override

Register push device with a unique identifier (FCM Token / OneSignal ID) This function will not be used usually, use changeUserId if you want

Implementation

@override
Future<DataResult> registerPushDeviceForUser({required String deviceToken}) async{
  if(isNullOrEmpty(deviceToken)) return DataResult(success: false, message: "Failed to register device due to missing device token", code: NUIPushCode.REGISTER_FAILED.name);

  final deviceInfo = await NUIDeviceUtil.getDeviceInfo();
  // _userId can be (email, username, or phone number)
  // TODO: include setting up the external Id to OneSignal using _universalId, so when third party system fire a push notification,
  // They can utilise the externalId (_universalId) to send notification to OneSignal, do not required back and forth query from iPush
  // Clear external ID when user logout with iAuth/ Mobile Application with SDK : OneSignal.shared.removeExternalId()
  final registerRequest = NUIRegisterPushDeviceRequest(
      userId: _userId,
      uniqueId: deviceInfo?.uuid ?? "",
      providerDeviceToken: deviceToken,
      deviceOs: deviceInfo?.platform,
      deviceModel: deviceInfo?.model
  );

  final result = await NUIWeb.get(module: NUIWEB_MODULE).doPOST<NUIStandardHttpResponse>(url: "push/registerNew", bodyEnt: registerRequest, entity: NUIStandardHttpResponseEntity());
  if(result.success == true){
    logNUI(_MODULE, "Register device successful");
    return DataResult(
        success: true,
        message: "Register device successful",
        code: NUIPushCode.REGISTER_SUCCESS.name
    );
  }
  else{
    logNUI(_MODULE, "Failed to register device");
    return DataResult(
        success: false,
        message: result.message,
        code: NUIPushCode.REGISTER_FAILED.name
    );
  }
}