一般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了,我们可以在\bin\Debug目录看到dll了,可以用反编译工具检查下dll是否有效
二、修改AndroidManifest配置
我的配置并不是和极光官方Android SDK的AndroidManifest配置一样,我参考了网上的别人的代码,配置如下:
"1.0" encoding="utf-8"?>"http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.LayoutDemo"> "21" android:targetSdkVersion="27" /> "android.permission.INTERNET" /> "android.permission.ACCESS_NETWORK_STATE" /> "com.companyname.LayoutDemo.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> "com.companyname.LayoutDemo.permission.JPUSH_MESSAGE" /> "android.permission.RECEIVE_USER_PRESENT" /> "android.permission.INTERNET" /> "android.permission.WAKE_LOCK" /> "android.permission.READ_PHONE_STATE" /> "android.permission.WRITE_EXTERNAL_STORAGE" /> "android.permission.READ_EXTERNAL_STORAGE" /> "android.permission.VIBRATE" /> "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> "android.permission.ACCESS_NETWORK_STATE" /> "android.permission.WRITE_SETTINGS" /> "android.permission.ACCESS_WIFI_STATE" /> "LayoutDemo" android:icon="@drawable/xamarin_logo"> "com.companyname.LayoutDemo.DataProvider" android:name="cn.jpush.android.service.DataProvider" android:exported="true" /> "cn.jpush.android.service.PushService" android:enabled="true" android:exported="false" > <service android:name="cn.jpush.android.service.DaemonService" android:enabled="true" android:exported="true">"cn.jpush.android.intent.REGISTER" /> "cn.jpush.android.intent.REPORT" /> "cn.jpush.android.intent.PushService" /> "cn.jpush.android.intent.PUSH_TIME" /> <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" > "cn.jpush.android.intent.DaemonService" /> "com.companyname.LayoutDemo"/> "1000"> "cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> "com.companyname.LayoutDemo"/> "android.intent.action.USER_PRESENT" /> "android.net.conn.CONNECTIVITY_CHANGE" /> <activity android:name="cn.jpush.android.ui.PushActivity" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.NoTitleBar" android:exported="false" > "android.intent.action.PACKAGE_ADDED" /> "android.intent.action.PACKAGE_REMOVED" /> "package" /> <activity android:name="cn.jpush.android.ui.PopWinActivity" android:configChanges="orientation|keyboardHidden" android:exported="false" android:theme="@android:style/Theme.NoTitleBar"> "cn.jpush.android.ui.PushActivity" /> "android.intent.category.DEFAULT" /> "com.companyname.LayoutDemo" /> <service android:name="cn.jpush.android.service.DownloadService" android:enabled="true" android:exported="false" > "android.intent.category.DEFAULT" /> "com.companyname.LayoutDemo" /> "cn.jpush.android.service.AlarmReceiver" /> "JPUSH_CHANNEL" android:value="developer-default"/> "JPUSH_APPKEY" android:value="替换成你的APPKEY"/>
把配置中所有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有点不一样,这里也需要替换包名com.companyname.LayoutDemo
[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)) { } } }
四、在极光推送网站注册,并测试发送
注意把所有的com.companyname.LayoutDemo替换成你自己的应用包名,应用包名必须和配置文件保持一致,极光推送根据应用包名和AppKey进行识别,这两个参数必须正确。
如果是真机测试,必须开启软件的通知权限。
坑:
项目在调试模式时,可以调用Jpush推送,但是打包APK之后发现消息推送接收不到,错误日志显示缺少libjcore126.so文件。
解决办法:在Android项目Assets文件夹下面加入libjcore126.so文件,并设置为AndroidNativeLibrary。 奇怪的是我新建一个项目,按照上述操作进行,无论调试模式还是打包APK都是OK的,不知道什么原因引起的。
参考文章:
https://www.jianshu.com/p/5abe3924acab
https://github.com/JimmyPun610/XamarinAndroidJiGuangPushNotification