龙吟虎啸是指什么生肖、成语释义解释落实 2025-10-22 03:43:12
魔卡战姬2025初夏庆典:幻境降临·星耀启程限时挑战活动 2025-05-16 03:18:51
萌卡11人五周年庆典狂欢月:集结最强阵容,赢取限定卡牌与豪华大礼包! 2025-05-31 17:18:34
战地雄心:2025年5月13日全球战场争霸赛盛大开启 2025-05-13 18:06:55
骑士泡泡龙:圣光之息跨服争霸赛暨盛夏狂欢盛典限时开启 2025-07-18 19:40:10
暗黑魔渊:深渊探索者挑战赛 2025-06-05 21:11:26
袁世凯自称麾下有九才人、十策士、十五大将,分别是谁? 2025-10-27 08:56:29
御天降魔传:2025年5月11日盛大开启的降魔之旅活动 2025-05-11 18:59:57
外出考察调研报告怎么写精选9篇 2025-10-01 23:55:36
户外照明类型和选择指南 2025-10-22 15:53:40

极光推送jpush(简单易懂,分分钟教你搞定)

先注册账户:

然后点击开发者服务:点击打开链接

创建应用:

随便起个名字,但是最好和你的应用名字一样

然后点击下一步推送设置

把你的工程应用名字输入:

应用包名就是build.gradle文件里的applicationId 名字

完成之后点击下载Demo

Demo下载完成之后解压 ,压缩包 将libs文件夹里的工具jar包全部复制到你的项目中,记得编译

将文件中的jar包导入工程中的libs文件夹 并引用, 在将res文件夹直接复制到项目中的src文件夹下的main文件夹里, 它会直接补齐你工程中缺少的部分,所以不用害怕它会替换掉你的原文件

使用 android studio 的开发者,如果使用 jniLibs 文件夹导入 so 文件,则仅需将所有 cpu 类型的文件夹拷进去;如果将 so 文件添加在 module的libs 文件夹下,注意在 module 的 gradle 配置中添加一下配置:

sourceSets {

main {

jniLibs.srcDirs = ['libs']

}

}

注意点,还有 jniLibs 空文件夹不要忘

MyApp 类(记得在清单文件中添加name)

public class MyApp extends Application {

public static String registrationId ;//获取 极光推送的设备唯一性标识 RegistrationID

@Override

public void onCreate() {

super.onCreate();

//极光推送

JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志

JPushInterface.init(this); // 初始化 JPush

registrationId = JPushInterface.getRegistrationID(this);//获取 极光推送的设备唯一性标识 RegistrationID

Log.e("111111registrationId", "run:--------->:" + registrationId );

}

}

MyReceiver

TestActivity

Logger

MyReceiver package com.sgy.sgy_jpush;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.text.TextUtils;

import org.json.JSONException;

import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**

* 自定义接收器

*

* 如果不定义这个 Receiver,则:

* 1) 默认用户会打开主界面

* 2) 接收不到自定义消息

*/

public class MyReceiver extends BroadcastReceiver {

private static final String TAG = "JIGUANG-Example";

@Override

public void onReceive(Context context, Intent intent) {

try {

Bundle bundle = intent.getExtras();

Logger.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {

String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);

Logger.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);

//send the Registration Id to your server...

} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {

Logger.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));

// processCustomMessage(context, bundle);

} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {

Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知");

int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);

Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {

Logger.d(TAG, "[MyReceiver] 用户点击打开了通知");

//打开自定义的Activity

Intent i = new Intent(context, TestActivity.class);

i.putExtras(bundle);

//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );

context.startActivity(i);

} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {

Logger.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));

//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..

} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {

boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);

Logger.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);

} else {

Logger.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());

}

} catch (Exception e){

}

}

// 打印所有的 intent extra 数据

private static String printBundle(Bundle bundle) {

StringBuilder sb = new StringBuilder();

for (String key : bundle.keySet()) {

if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {

sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));

}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){

sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));

} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {

if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {

Logger.i(TAG, "This message has no Extra data");

continue;

}

try {

JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));

Iterator it = json.keys();

while (it.hasNext()) {

String myKey = it.next();

sb.append("\nkey:" + key + ", value: [" +

myKey + " - " +json.optString(myKey) + "]");

}

} catch (JSONException e) {

Logger.e(TAG, "Get message extra JSON error!");

}

} else {

sb.append("\nkey:" + key + ", value:" + bundle.get(key));

}

}

return sb.toString();

}

//send msg to MainActivity

// private void processCustomMessage(Context context, Bundle bundle) {

// if (MainActivity.isForeground) {

// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);

// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

// Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);

// msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);

// if (!ExampleUtil.isEmpty(extras)) {

// try {

// JSONObject extraJson = new JSONObject(extras);

// if (extraJson.length() > 0) {

// msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);

// }

// } catch (JSONException e) {

//

// }

//

// }

// LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);

// }

// }

}

TestActivity package com.sgy.sgy_jpush;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.ViewGroup.LayoutParams;

import android.widget.TextView;

import cn.jpush.android.api.JPushInterface;

public class TestActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView tv = new TextView(this);

tv.setText("用户自定义打开的Activity");

Intent intent = getIntent();

if (null != intent) {

Bundle bundle = getIntent().getExtras();

String title = null;

String content = null;

if(bundle!=null){

title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);

content = bundle.getString(JPushInterface.EXTRA_ALERT);

}

tv.setText("Title : " + title + " " + "Content : " + content);

}

addContentView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

}

}

Logger package com.sgy.sgy_jpush;

import android.util.Log;

/**

* Created by efan on 2017/4/13.

*/

public class Logger {

//设为false关闭日志

private static final boolean LOG_ENABLE = true;

public static void i(String tag, String msg){

if (LOG_ENABLE){

Log.i(tag, msg);

}

}

public static void v(String tag, String msg){

if (LOG_ENABLE){

Log.v(tag, msg);

}

}

public static void d(String tag, String msg){

if (LOG_ENABLE){

Log.d(tag, msg);

}

}

public static void w(String tag, String msg){

if (LOG_ENABLE){

Log.w(tag, msg);

}

}

public static void e(String tag, String msg){

if (LOG_ENABLE){

Log.e(tag, msg);

}

}

}

清单文件:加权限:

android:name="com.sgy.sgy_jpush.permission.JPUSH_MESSAGE"

android:protectionLevel="signature" />

android:name="cn.jpush.android.ui.PopWinActivity"

android:theme="@style/MyDialogStyle"

android:exported="false">

android:name="cn.jpush.android.ui.PushActivity"

android:configChanges="orientation|keyboardHidden"

android:theme="@android:style/Theme.NoTitleBar"

android:exported="false">

android:name="cn.jpush.android.service.PushService"

android:process=":pushcore"

android:exported="false">

android:authorities="com.sgy.sgy_jpush.DataProvider"

android:name="cn.jpush.android.service.DataProvider"

android:process=":pushcore"

android:exported="false"

/>

android:name="cn.jpush.android.service.DaemonService"

android:enabled="true"

android:exported="true">

android:authorities="com.sgy.sgy_jpush.DownloadProvider"

android:name="cn.jpush.android.service.DownloadProvider"

android:exported="true"

/>

android:name="cn.jpush.android.service.PushReceiver"

android:enabled="true"

android:exported="false">

android:name="com.sgy.sgy_jpush.MyReceiver"

android:exported="false"

android:enabled="true">

然后运行一下工程:

在回到极光平台:点击打开链接

哈哈哈,然后你就可以收到推送的消息啦,是不是很简单呢!!!

最后在附上完整的AndroidManifest.xml清单文件: