美文网首页xamarin开发技术
Xamarin.iOS消息推送(AzurePushNotific

Xamarin.iOS消息推送(AzurePushNotific

作者: Funky_Xamarin | 来源:发表于2017-09-19 16:53 被阅读74次

本文主要讲解在Xamarin.iOS开发中使用AzurePushNotifications来集成消息推送功能


本文主要讲解一个在实际项目中使用非常方便、集成推送第三方的第三方库,此前我已经介绍过了极光推送的集成介绍,但是大部分注册代码都需要我们自己去完成,但今天给大家介绍的AzurePushNotifications的库简化了注册和使用部分,但是需要我们用户Azure的后台,也就是说我们服务器也需要根据Azure来集成,对于Azure的介绍大家可以百度看看。

首先我们看看这个第三方库的源码,我们首先向APNS注册Azure Push Notification,这里作者给我们封装好了一个方法:

public void RegisterForAzurePushNotification()
{
     if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
     {
         var authOptions = UserNotifications.UNAuthorizationOptions.Alert | UserNotifications.UNAuthorizationOptions.Badge | UserNotifications.UNAuthorizationOptions.Sound;
           UserNotifications.UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
                {
                    Console.WriteLine(granted);
                });
                UIApplication.SharedApplication.RegisterForRemoteNotifications();
      }
      else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
      {
                var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());
               UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
                UIApplication.SharedApplication.RegisterForRemoteNotifications();
      }
      else
      {
                var notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
      }
}

看了上述的代码后,我们只需要在我们希望注册推送的地方添加以下代码:

PushNotificationCredentials.Tags = new string[] { };
PushNotificationCredentials.GoogleApiSenderId = "google sender id";
PushNotificationCredentials.AzureNotificationHubName = "hub name";
PushNotificationCredentials.AzureListenConnectionString = "Endpoint";

CrossAzurePushNotifications.Current.RegisterForAzurePushNotification();

CrossAzurePushNotifications.Current.OnMessageReceived += (sender, ev) =>
{
    Debug.WriteLine(ev.Content);
};

上述代码可以在任何地方添加,也就是说你可以在用户登录或者根据用户设置再注册推送。


接着我们还需要向APNS注册我们的设备信息,这里我们也可以看到这个第三方库已经帮我们写好了这些内容(如果你不明白这样的工作原理的话,可以看看我上一篇极光推送集成里面对于推送的介绍)

public void RegisteredForRemoteNotifications(NSData deviceToken)
{
      Console.WriteLine("deviceToken: " + deviceToken);

      Hub = new SBNotificationHub(PushNotificationCredentials.AzureListenConnectionString, PushNotificationCredentials.AzureNotificationHubName);

       Hub.UnregisterAllAsync(deviceToken, error =>
       {
            if (error != null)
            {
                Console.WriteLine("Error calling Unregister: {0}", error.ToString());
                return;
            }

            NSSet tags = new NSSet(PushNotificationCredentials.Tags);

           Hub.RegisterNativeAsync(deviceToken, tags, errorCallback =>
           {
                if (errorCallback != null)
                {
                    Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
                }
           });
     });
}

同样我们在了解这段代码的作用后,我们只需要在AppDelegate中进行重写一下两个方法:

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
    CrossAzurePushNotifications.Platform.ProcessNotification(userInfo);
}

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    CrossAzurePushNotifications.Platform.RegisteredForRemoteNotifications(deviceToken);
}

这样我们就可以实现推送的集成,然后我们通过Azure后台发送推送,在真机上就能上收到相应的推送了。这里多介绍一点,这个类库的作者在ProcessNotification(userInfo);的处理取消了弹窗提醒,这里的点击推送消息处理大家可以根据自己的业务需求进行更改,作者的源码只是给了大家一种处理的借鉴,但实际项目中并不推荐。
这里给出一种根据推送过来的json内容不同进行不同业务处理的一个模板,大家可以参考进行自己业务的处理。

private void ProcessNotification(NSDictionary options)
{
      if (null != options && options.ContainsKey(new NSString("dataType")) && options.ContainsKey(new NSString("data")))
      {
           var datatype = options.ObjectForKey(new NSString("dataType")) as NSString;
           var data = options.ObjectForKey(new NSString("data")) as NSString;
           //do your custom navigation here
      }
}

同时作者在这里提供了取消注册AzurePushNotification的方法,我们可以控制用户在未登录状态下不让用户收到推送。

public void UnregisterFromAzurePushNotification()
{
    UIApplication.SharedApplication.UnregisterForRemoteNotifications();
}

到这里Xamarin.iOS消息推送使用AzurePushNotifications的介绍就完成了,希望能对您有所帮助。

——End 有问题可以加我微信,大家一起讨论

相关文章

  • Xamarin.iOS消息推送(AzurePushNotific

    本文主要讲解在Xamarin.iOS开发中使用AzurePushNotifications来集成消息推送功能 本文...

  • Xamarin.iOS实现消息推送(极光推送)

    在实际APP开发过程中,消息推送是一个不可取少的功能,这里我们介绍在Xamarin.IOS项目中如何实现消息推送 ...

  • 消息推送

    推送通知的呈现效果总结 推送通知有5种不同的呈现效果1、在屏幕顶部显示一块横幅(显示具体内容)2、在屏幕中间弹出一...

  • 消息推送

    1、.简述一下消息推送的流程 2、如果不依赖APNS,如何在App进入后台时保证能够接收到远程消息?

  • 消息推送

    推送概念? APNs:Apple Push Notification Service,远程通知只能APNs服务器发...

  • 消息推送

    本地推送 @implementation AppDelegate 远程推送http://blog.csdn.net...

  • 消息推送

  • 消息推送

    http://blog.csdn.net/showhilllee/article/details/8631734

  • 消息推送

    https://www.jianshu.com/p/c58f8322a278

  • 消息推送

    1 什么是消息推送 很多手机APP会不定时的给用户推送消息,例如一些新闻APP会给用户推送用户可能感兴趣的新闻,或...

网友评论

    本文标题:Xamarin.iOS消息推送(AzurePushNotific

    本文链接:https://www.haomeiwen.com/subject/vvzwsxtx.html