发送pussh跨平台,枢纽Azure

时间:2018-07-02 13:01:09

标签: java c# android azure

我有一个正在为IOS开发的应用程序。我需要将它们推到平台之间。就像今天写的那样,它在后端可以在Android for Android上运行,但是我更改了设备的注册,以便将它们注册到(每个平台的)相应模板中。

更多,它显然停止工作,因为它是从Android编写到Android的。 集线器中的记录是正确的。要发送推送,我应该更改什么?

后端代码:

 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");
            }
        }

设备代码:

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();
    }
}

0 个答案:

没有答案