您的位置:首页 > 移动开发 > Android开发

极光推送的使用

2016-07-03 14:30 399 查看
极光推送(自己写应用测试时不太灵敏,有时候会接收不到推送(普通用户情况下))
https://www.jpush.cn
极光推送

通过极光推送服务,主动、及时地向您的用户发起交互,向其发送聊天消息、

日程提醒、活动预告、进度提示、动态更新等。

精准的目标用户和有价值的推送内容可以提升用户忠诚度,提高留存率与收入。

登陆极光推送,创建应用名称和应用包名。

下载JPushExample

Error:(9, 1) A problem occurred evaluating root project 'JPushExample(483504)'.

> Plugin with id 'com.android.application' not found.

buildscript {

    repositories {

        jcenter()

    }

    dependencies {

        classpath 'com.android.tools.build:gradle:1.5.0'

    }

}

把 classpath 'com.android.tools.build:gradle:1.5.0'前面的注释去掉,或者直接换上自己的classpath.

极光推送官方文档:docs.jpush.io

07-03 11:03:10.864 31108-31108/? E/JPush: [JPushInterface] 

System.loadLibrary::jpush217java.lang.UnsatisfiedLinkError: 

dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.just.push-

2/base.apk"],nativeLibraryDirectories=[/data/app/com.just.push-2/lib/arm64, 

/vendor/lib64, /system/lib64]]] couldn't find "libjpush217.so"

按照极光推送 官方集成文档 出现 出现获取sdk版本失败的问题 JPush .so file do not match  JPush .jar file in the project

可以编辑 build.gradle 脚本 添加

android{

sourceSets {

        main {

            jniLibs.srcDirs = ['libs']

        }

    }

......

.....

}

高级功能:

接收推送消息Receiver

使用推送也可以用来实现记录类似微信新消息数量的功能
//在自定义消息中可以发送json类型的数据

注意点:

所有需要客户端被动接收信息的功能模块,都可以用推送实现。

极光推送设置应用信息的图片:



下载JpushExample



Android Studio中清单文件配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.just.push">

<!--在AndroidManifest中使用 ${applicationId} 引用gradle中定义的包名-->
<!--Required-->
<permission
android:name="${applicationId}.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />

<!-- Required 一些系统要求的权限,如访问网络等-->
<uses-permission android:name="${applicationId}.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.WRITE_SETTINGS" />
<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.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<!-- Optional for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:name=".MyApplication"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- Rich push 核心功能 since 2.0.6-->
<activity
android:name="cn.jpush.android.ui.PopWinActivity"
android:theme="@style/MyDialogStyle"
android:exported="false">
</activity>

<!-- 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="${applicationId}" />
</intent-filter>
</activity>
<!-- Required SDK核心功能-->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false" >
</service>

<!-- 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="${applicationId}" />
</intent-filter>

</service>

<!-- Required SDK核心功能-->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true"
android:exported="false">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!--Required 显示通知栏 -->
<category android:name="${applicationId}" />
</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核心功能-->
<receiver android:name="cn.jpush.android.service.AlarmReceiver" android:exported="false"/>

<!-- User defined. For test only 用户自定义的广播接收器-->
<receiver
android:name="com.just.push.PushReceiver"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required 用户注册SDK的intent-->
<action android:name="cn.jpush.android.intent.UNREGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required 用户接收SDK消息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required 用户接收SDK通知栏信息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required 用户打开自定义通知栏的intent-->
<action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!--Optional 用户接受Rich Push Javascript 回调函数的intent-->
<action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 -->
<category android:name="${applicationId}" />
</intent-filter>
</receiver>

<!-- Required . Enable it you can get statistics data with channel -->
<meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
<meta-data android:name="JPUSH_APPKEY" android:value="6efdce4984a981d392562915" /> <!-- </>值来自开发者平台取得的AppKey-->
</application>

</manifest>


全局Application中代码编写
package com.just.push;

import android.app.Application;

import cn.jpush.android.api.JPushInterface;

/**
* Application天然是单例的,我们可以把一些全局信息和全局的方法写在Application中。
* Created by Administrator on 2016/7/2 0002.
*/
public class MyApplication extends Application {

/**
* 这边应用一创建就会调用JPush方法
*/
@Override
public void onCreate() {
super.onCreate();
System.out.println("应用被创建了");
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}


Receiver中代码编写
package com.just.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
* Created by Administrator on 2016/7/2 0002.
*/
public class PushReceiver extends BroadcastReceiver {
private static final String TAG = "PushReceiver";

//当受到消息,会回调onReceive()方法,onReceive中解析
@Override
public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();
Log.d(TAG, "onReceive - " + intent.getAction());

//注册
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {

//收到自定义消息
}else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
//自定义消息可以发送json数据
System.out.println("收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
// 自定义消息不会展示在通知栏,完全要开发者写代码去处理
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
System.out.println("收到了通知");
// 在这里可以做些统计,或者做些其他工作(通知栏被点击)
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
System.out.println("用户点击打开了通知");
// 在这里可以自己写代码去定义用户点击后的行为
//保存服务器推送下来的附加字段。这是个 JSON 字符串。
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
System.out.println("附加信息:"+extras);
try {
JSONObject obj = new JSONObject(extras);
String url = obj.getString("url");
System.out.println("url:"+url);
//跳浏览器
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息