xamarin学习系列之极光消息推送(代码片段)

fengchao1000 fengchao1000     2023-01-31     142

关键词:

一般App都会有消息推送的功能,如果是原生安卓或者IOS集成消息推送很容易,各大推送平台都有相关的Sample,但是关于Xamarin.Forms的消息推送集成的资料非常少,下面就说下Xamarin.Forms(Android)怎么集成极光推送

准备工作:

1、了解极光推送原理:https://docs.jiguang.cn/jpush/client/Android/android_sdk/

2、下载极光官方Android SDK :https://docs.jiguang.cn/jpush/resources//

3、注册极光账号,拿到AppKey和Master Secret

4、学习xamarin绑定jar包 :https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/binding-a-jar/

以上是准备工作,熟悉以上的内容之后,我们正式进入极光推送集成

 

一、把极光官方的jar包转换成Dll

1、建立一个android Bindings Libary项目

技术分享图片

2、解压极光官方Android SDK,我下载的版本是3.1.7

技术分享图片

3、把jar包和so文件拷入对应文件夹下面,并设置Build Action,jar文件的Build Action设置为EmbeddedJar,so文件的Build Action设置为EmbeddedNativeLibrary,然后编译就OK了,我们可以在inDebug目录看到dll了,可以用反编译工具检查下dll是否有效

技术分享图片

技术分享图片

 

二、修改AndroidManifest配置

我的配置并不是和极光官方Android SDK的AndroidManifest配置一样,我参考了网上的别人的代码,配置如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.LayoutDemo">
  <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" /> 
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <!--JPush Required-->
  <permission  android:name="com.companyname.LayoutDemo.permission.JPUSH_MESSAGE"  android:protectionLevel="signature" />
  <uses-permission android:name="com.companyname.LayoutDemo.permission.JPUSH_MESSAGE" />
  <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
  
  <application android:label="LayoutDemo" android:icon="@drawable/xamarin_logo"> 
    <!-- since 3.0.9 Required SDK-->
    <provider   android:authorities="com.companyname.LayoutDemo.DataProvider"   android:name="cn.jpush.android.service.DataProvider" android:exported="true"  />
    <!-- Required SDK 核心功能-->
    <!-- 可配置android:process参数将PushService放在其他进程中 -->
    <service   android:name="cn.jpush.android.service.PushService"     android:enabled="true"  android:exported="false" >
      <intent-filter>
        <action android:name="cn.jpush.android.intent.REGISTER" />
        <action android:name="cn.jpush.android.intent.REPORT" />
        <action android:name="cn.jpush.android.intent.PushService" />
        <action android:name="cn.jpush.android.intent.PUSH_TIME" />
      </intent-filter>
    </service> 
    <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->
    <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->
    <service
        android:name="cn.jpush.android.service.DaemonService"
        android:enabled="true"
        android:exported="true">
      <intent-filter >
        <action android:name="cn.jpush.android.intent.DaemonService" />
        <category android:name="com.companyname.LayoutDemo"/>
      </intent-filter>
    </service> 
    <!-- Required SDK核心功能-->
    <receiver
        android:name="cn.jpush.android.service.PushReceiver"
        android:enabled="true" >
      <intent-filter android:priority="1000">
        <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
        <category android:name="com.companyname.LayoutDemo"/>
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.USER_PRESENT" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
      </intent-filter>
      <!-- Optional-->
      <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
      </intent-filter>
    </receiver> 
    <!-- Required SDK核心功能-->
    <activity
        android:name="cn.jpush.android.ui.PushActivity"
        android:configChanges="orientation|keyboardHidden"
        android:theme="@android:style/Theme.NoTitleBar"
        android:exported="false" >
      <intent-filter>
        <action android:name="cn.jpush.android.ui.PushActivity" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="com.companyname.LayoutDemo" />
      </intent-filter>
    </activity>
    <!-- SDK核心功能-->
    <activity
        android:name="cn.jpush.android.ui.PopWinActivity"
        android:configChanges="orientation|keyboardHidden"
        android:exported="false"
        android:theme="@android:style/Theme.NoTitleBar">
      <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="com.companyname.LayoutDemo" />
      </intent-filter>
    </activity> 
    <!-- Required SDK核心功能-->
    <service
        android:name="cn.jpush.android.service.DownloadService"
        android:enabled="true"
        android:exported="false" >
    </service> 
    <!-- Required SDK核心功能-->
    <receiver android:name="cn.jpush.android.service.AlarmReceiver" /> 
    <!-- Required. For publish channel feature -->
    <!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。-->
    <!-- 例如: -->
    <!-- 发到 Google Play 的APK可以设置为 google-play; -->
    <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
    <!-- 目前这个渠道统计功能的报表还未开放。-->
    <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
    <!-- Required. AppKey copied from Portal -->
    <meta-data android:name="JPUSH_APPKEY" android:value="替换成你的APPKEY"/>
  </application>
 
</manifest>

把配置中所有com.companyname.LayoutDemo替换成你自己的app包名,然后替换Appkey,就ok了

 

三、在Android 项目的MainActivity中初始化PushNotification

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    
        protected override void OnCreate(Bundle savedInstanceState)
        
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            initPushNotification();

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        

        private void initPushNotification()
        
            IntentFilter filter = new IntentFilter();
            filter.AddAction(JPushInterface.ActionNotificationOpened);
            filter.AddAction(JPushInterface.ActionNotificationReceived);
            filter.AddAction(JPushInterface.ActionMessageReceived);
            filter.AddAction(JPushInterface.ActionRegistrationId);
            filter.AddAction(JPushInterface.ActionConnectionChange);
            NotificationReceiver receiver = new NotificationReceiver();
            RegisterReceiver(receiver, filter);
            JPushInterface.SetDebugMode(true);
            JPushInterface.Init(this.ApplicationContext);
        

    

添加NotificationReceiver类,把配置中的IntentFilter放在了代码中,这里和官方sample有点不一样

[BroadcastReceiver]
    [IntentFilter(new string[]  "cn.jpush.android.intent.REGISTRATION" , Categories = new string[]  "com.companyname.LayoutDemo" )]
    [IntentFilter(new string[]  "cn.jpush.android.intent.MESSAGE_RECEIVED" , Categories = new string[]  "com.companyname.LayoutDemo" )]
    [IntentFilter(new string[]  "cn.jpush.android.intent.NOTIFICATION_RECEIVED" , Categories = new string[]  "com.companyname.LayoutDemo" )]
    [IntentFilter(new string[]  "cn.jpush.android.intent.NOTIFICATION_OPENED" , Categories = new string[]  "com.companyname.LayoutDemo" )]
    [IntentFilter(new string[]  "cn.jpush.android.intent.CONNECTION" , Categories = new string[]  "com.companyname.LayoutDemo" )]
    class NotificationReceiver : PushReceiver
    
        public override void OnReceive(Context context, Intent intent)
        
            base.OnReceive(context, intent);

            //当点击消息时进入,进入对应的页面
            if (intent.Action == JPushInterface.ActionNotificationOpened)
            
                //When user tap the notification on notification center
                Bundle bundle = intent.Extras;
                string jsonData = bundle.GetString(JPushInterface.ExtraExtra);
            
            //第一次安装app时进入,会拿到registrationID,保存registrationID,当用户登录之后把用户id和registrationID关联,方便之后一对一发送消息
            if (intent.Action == JPushInterface.ActionRegistrationId)
            
                //Only call when first launch, get the registrationID
                string regID = JPushInterface.GetRegistrationID(context);
            
            //当接收到信息时进入,弹出消息框
            if (JPushInterface.ActionMessageReceived.Equals(intent.Action))
            
                
            

 //当接收到信息时进入,弹出消息框
            if (JPushInterface.ActionNotificationReceived.Equals(intent.Action))
              

             
            if (JPushInterface.ActionNotificationReceived.Equals(intent.Action))
             

            
        

    

 

 四、在极光推送网站注册,并测试发送

技术分享图片

注意应用包名必须和配置文件保持一致,极光推送根据应用包名和AppKey进行识别,这两个参数必须正确。

如果是真机测试,必须开启软件的通知权限。

 

参考文章:

https://www.jianshu.com/p/5abe3924acab

https://github.com/JimmyPun610/XamarinAndroidJiGuangPushNotification

flutter学习日记之集成极光推送(代码片段)

本文地址:https://blog.csdn.net/qq_40785165/article/details/122746461,转载需附上此链接每一个闪闪发光的人都在背后熬过了一个又一个不为人知的黑夜大家好,我是小黑,一个还没秃头的程序员~~~本次介绍的是在Flutter中集... 查看详情

flutter学习日记之集成极光推送(代码片段)

本文地址:https://blog.csdn.net/qq_40785165/article/details/122746461,转载需附上此链接每一个闪闪发光的人都在背后熬过了一个又一个不为人知的黑夜大家好,我是小黑,一个还没秃头的程序员~~~本次介绍的是在Flutter中集... 查看详情

极光推送的使用(代码片段)

极光推送的使用 苹果的APNSimage.png用户的应用注册了APNS消息推送功能用户iOS设备通过SSL长连接到APNS苹果服务器,收到设备应用的注册信息后,下发给设备一个DeviceToken给应用应用收到这个DeviceToken然后推送给自己应用的服务器(... 查看详情

极光推送jpush简介集成(代码片段)

JPush产品简介控制台:https://www.jiguang.cn/dev/#/app/list#dev极光推送是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,让开发者积极地保持与用户的连接,从而提高用户活跃度、提高应用的留存率。主... 查看详情

极光推送工具类(代码片段)

...遇到服务器向App推送消息的需求,一般企业中选择用极光推送的比较多,在集成极光时发现极光的文档并不完整,网上的文章也很多不能直接使用,这里列出我在工作中集成极光的全部代码,只需要按照如下... 查看详情

极光推送消息——registrationid方式(代码片段)

1、工具类packagecom.test.util;importcn.jiguang.common.resp.APIConnectionException;importcn.jiguang.common.resp.APIRequestException;importcn.jpush.api.JPushClient;importcn.jpush.api.push.PushResult;importcn.jpush.api.push.model.Message;importcn.jpush.api.push.model.Options;importcn.jpush.api.push... 查看详情

php实现app消息推送(代码片段)

...很多的消息推送厂商,比如阿里云的消息推送,极光推送,融云的消息推送。他们的原理都是把sdk内置在app里面,达到消息推送的目的,通过一张图来了解一下,看不懂不要紧,理解大概的过程就行实... 查看详情

php实现app消息推送(代码片段)

...很多的消息推送厂商,比如阿里云的消息推送,极光推送,融云的消息推送。他们的原理都是把sdk内置在app里面,达到消息推送的目的,通过一张图来了解一下,看不懂不要紧,理解大概的过程就行实... 查看详情

springcloud学习系列之五-----配置中心(config)和消息总线(bus)完美使用版(代码片段)

前言在上篇中介绍了SpringCloudConfig的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloudFinchley版)中的分布式配置中心(SpringCloudConfig)的配置刷新和消息总线(RabbitMQ和Kafka)使用教程。SpringCloudConfigRefresh在上一篇中我们介绍了spri... 查看详情

springcloud学习系列之五-----配置中心(config)和消息总线(bus)完美使用版(代码片段)

前言在上篇中介绍了SpringCloudConfig的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloudFinchley版)中的分布式配置中心(SpringCloudConfig)的配置刷新和消息总线(RabbitMQ和Kafka)使用教程。SpringCloudConfigRefresh在上一篇中我们介绍了spri... 查看详情

重学springboot系列之服务器推送技术(代码片段)

...用户向其他用户群发websocket实现聊天软件测试websocke深入学习资料即时通信IMGoeasy详解消息系统新手入门:史上最全Web端即时通讯技术原理详解主流服务器推送技术说明需求与背景若干年前,所有的请求都是由浏览器端发... 查看详情

极光推送工具类(代码片段)

...遇到服务器向App推送消息的需求,一般企业中选择用极光推送的比较多,在集成极光时发现极光的文档并不完整,网上的文章也很多不能直接使用,这里列出我在工作中集成极光的全部代码,只需要按照如下... 查看详情

厂商推送限制说明及极光的适配方案(代码片段)

背景自2023年起,各个厂商逐步对营销类消息做数量管控,具体如下:华为自2023年1月5日起,华为PUSH通道将根据应用类型对资讯营销类消息的每日推送数量进行上限管理,服务与通讯类消息每日推送数量不受限... 查看详情

android一分钟快速使用极光推送(代码片段)

...果你是老手,每个人都各有各的风格习惯,相互学习的可以了解下如果你是新手,个人觉得以下的集成方式应该会帮到你避免很多坑极光推送官方文档集成方式1、jcenter自动集成2、手动集成 查看详情

kafka系列一之架构介绍和安装(代码片段)

Kafka架构介绍和安装写在前面还是那句话,当你学习一个新的东西之前,你总得知道这个东西是什么?这个东西可以用来做什么?然后你才会去学习它,使用它。简单来说,kafka既是一个消息队列,如今,它也演变为一个分布式... 查看详情

jpush之如何利用httpclient实现消息推送到手机

...踩右脚上试试.只为逗你一笑】提出问题后端如何利用Jpush极光推送消息到手机端???解决问题Jpush极光文档官网http://docs.jiguang.cn/jpush/guideline/intro/Jpush产品简介JPush 查看详情

jpush的集成步骤及相关原理(代码片段)

第三方推送:极光推送的集成苹果官方的APNS推送,想必大家并不陌生,它有一些不足之处就是:到达率低,推送效率慢,开发维护运营比较高;对于它的这些不足之处,才产生了受大家欢迎的第三... 查看详情

app的消息推送(极光推送)

APP的消息推送,使用的第三方平台是极光推送 简单案例(以Thinkphp为例):1、下载下载PHPSDK2、把PHPSDK目录下的jpush-api-php-client-3.5.1srcJPush,Jpush下全部文件复制到项目中:ThinkPHPLibraryOrgJPush3、thinkphp中Org下面的文件会自动加载... 查看详情