H
Henrique Mendes Carvalho
Guest
I have an application that is now being developed for IOS. I need them pushed between the platforms. As it is written today, in the backend, it works from Android for Android, but I changed the registration of the devices, so that they were registered with the respective templates (of each platform).
More, it stopped working obviously because it is written from Android to Android. The record in the hub is correct. What should I change for push to be sent?
Backend code:
public static async void enviarPushNotification(ApiController controller, DataObjects.Notification notification) { // Get the settings for the server project. HttpConfiguration config = controller.Configuration; MobileAppSettingsDictionary settings = controller.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); // Get the Notification Hubs credentials for the Mobile App. string notificationHubName = settings.NotificationHubName; string notificationHubConnection = settings .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString; // Create a new Notification Hub client. NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName); // Android payload JObject data = new JObject(); data.Add("Id", notification.Id); data.Add("Descricao", notification.Descricao); data.Add("Tipo", notification.Tipo); data.Add("Id_usuario", notification.Id_usuario); //data.Add("Informacao", notification.Informacao); data.Add("Informacao", notification.Informacao); data.Add("Status", notification.Status); //alteração com a colocação da tag priority em caso de erro teste sem \"priority\": \"high\" var androidNotificationPayload = "{ \"priority\": \"high\", \"data\" : {\"message\":" + JsonConvert.SerializeObject(data) + "}}"; // var androidNotificationPayload = "{ \"data\" : {\"message\":" + JsonConvert.SerializeObject(data) + "}}"; try { // Send the push notification and log the results. String tag = "_UserId:" + notification.Id_usuario; //var result = await hub.SendGcmNativeNotificationAsync(androidNotificationPayload); var result = await hub.SendGcmNativeNotificationAsync(androidNotificationPayload, tag); // Write the success result to the logs. config.Services.GetTraceWriter().Info(result.State.ToString()); } catch (System.Exception ex) { // Write the failure result to the logs. config.Services.GetTraceWriter().Error(ex.Message, null, "Push.SendAsync Error"); } }
Device code(Example, Android):
public class RegistrationIntentService extends IntentService { private static final String TAG = "RegIntentService"; private NotificationHub hub; public RegistrationIntentService() { super(TAG); } public ApplicationUtils getApplicationUtils() { return (ApplicationUtils) getApplication(); } @override protected void onHandleIntent(Intent intent) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); try { if (FirebaseInstanceId.getInstance() == null) { FirebaseApp.initializeApp(this); } String FCM_token = FirebaseInstanceId.getInstance().getToken(); SaveSharedPreferences.setFCM(getApplicationContext(), FCM_token); String registrationID = sharedPreferences.getString("registrationID", null); if (registrationID == null) { registerDevice(sharedPreferences, FCM_token); } else { String fcMtoken = sharedPreferences.getString("FCMtoken", ""); if (!fcMtoken.equals(FCM_token)) { registerDevice(sharedPreferences, FCM_token); } } } catch (Exception e) { Log.e(TAG, "Failed to complete registration", e); } } private void registerDevice(SharedPreferences sharedPreferences, String FCM_token) throws Exception { String tag = "_UserId:" + getApplicationUtils().getUsuario().Id; NotificationHub hub = new NotificationHub(NotificationSettings.HubName, NotificationSettings.HubListenConnectionString, this); String templateBodyGCM = "{\"data\":{\"message\":\"$(messageParam)\"}}"; TemplateRegistration registration = hub.registerTemplate(FCM_token, "simpleGCMTemplate", templateBodyGCM, tag); String regID = registration.getRegistrationId(); sharedPreferences.edit().putString("registrationID", regID).apply(); sharedPreferences.edit().putString("FCMtoken", FCM_token).apply(); }}
It works normally android> android or ios> android, but when sending Android> iOS, the push does not arrive.
Continue reading...
More, it stopped working obviously because it is written from Android to Android. The record in the hub is correct. What should I change for push to be sent?
Backend code:
public static async void enviarPushNotification(ApiController controller, DataObjects.Notification notification) { // Get the settings for the server project. HttpConfiguration config = controller.Configuration; MobileAppSettingsDictionary settings = controller.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); // Get the Notification Hubs credentials for the Mobile App. string notificationHubName = settings.NotificationHubName; string notificationHubConnection = settings .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString; // Create a new Notification Hub client. NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName); // Android payload JObject data = new JObject(); data.Add("Id", notification.Id); data.Add("Descricao", notification.Descricao); data.Add("Tipo", notification.Tipo); data.Add("Id_usuario", notification.Id_usuario); //data.Add("Informacao", notification.Informacao); data.Add("Informacao", notification.Informacao); data.Add("Status", notification.Status); //alteração com a colocação da tag priority em caso de erro teste sem \"priority\": \"high\" var androidNotificationPayload = "{ \"priority\": \"high\", \"data\" : {\"message\":" + JsonConvert.SerializeObject(data) + "}}"; // var androidNotificationPayload = "{ \"data\" : {\"message\":" + JsonConvert.SerializeObject(data) + "}}"; try { // Send the push notification and log the results. String tag = "_UserId:" + notification.Id_usuario; //var result = await hub.SendGcmNativeNotificationAsync(androidNotificationPayload); var result = await hub.SendGcmNativeNotificationAsync(androidNotificationPayload, tag); // Write the success result to the logs. config.Services.GetTraceWriter().Info(result.State.ToString()); } catch (System.Exception ex) { // Write the failure result to the logs. config.Services.GetTraceWriter().Error(ex.Message, null, "Push.SendAsync Error"); } }
Device code(Example, Android):
public class RegistrationIntentService extends IntentService { private static final String TAG = "RegIntentService"; private NotificationHub hub; public RegistrationIntentService() { super(TAG); } public ApplicationUtils getApplicationUtils() { return (ApplicationUtils) getApplication(); } @override protected void onHandleIntent(Intent intent) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); try { if (FirebaseInstanceId.getInstance() == null) { FirebaseApp.initializeApp(this); } String FCM_token = FirebaseInstanceId.getInstance().getToken(); SaveSharedPreferences.setFCM(getApplicationContext(), FCM_token); String registrationID = sharedPreferences.getString("registrationID", null); if (registrationID == null) { registerDevice(sharedPreferences, FCM_token); } else { String fcMtoken = sharedPreferences.getString("FCMtoken", ""); if (!fcMtoken.equals(FCM_token)) { registerDevice(sharedPreferences, FCM_token); } } } catch (Exception e) { Log.e(TAG, "Failed to complete registration", e); } } private void registerDevice(SharedPreferences sharedPreferences, String FCM_token) throws Exception { String tag = "_UserId:" + getApplicationUtils().getUsuario().Id; NotificationHub hub = new NotificationHub(NotificationSettings.HubName, NotificationSettings.HubListenConnectionString, this); String templateBodyGCM = "{\"data\":{\"message\":\"$(messageParam)\"}}"; TemplateRegistration registration = hub.registerTemplate(FCM_token, "simpleGCMTemplate", templateBodyGCM, tag); String regID = registration.getRegistrationId(); sharedPreferences.edit().putString("registrationID", regID).apply(); sharedPreferences.edit().putString("FCMtoken", FCM_token).apply(); }}
It works normally android> android or ios> android, but when sending Android> iOS, the push does not arrive.
Continue reading...