Android之极光推送SDK集成和基础功能的实现

一:效果图

 

二:实现步骤

极光推送官网网址:https://www.jiguang.cn/push

1.如果有账号直接登录,没有的自己注册,就不用多说这个问题了。

注意一点

这里的包名必须一定及肯定得和注册文件AndroidManifest的包名一样,不然后面就不用做了、

 

2.在官网下载Android的SDK文件,下载解压打开就是以下目录、

 

 example是一个演示的demo,libs文件里面就是我们需要的so文件和jar文件、

 

3.根据你的需求复制相应的so文件,这里一般常用就是arm64-v8a,armeabi,armeabi-v7a三个文件,有些需要用到x86和x86_64的也复制进去,然后再把那两个jar文件复制到libs目录下,jar文件点击右键添加引用进去、

4.复制res里面的文件、

drawable_hdpi里面有图片和drawable的文件,你可以都复制到drawable文件下,规范一点也可以分开复制到相应的文件里,然后就是layout布局复制到相应的文件,values里面就是一个样式,也复制到相应的文件里去,也可以复制里面的代码进去,看自己的心情和爱好就不多说这个问题了、

 

4.在项目主工程build.gradle下添加一句代码引用工程libs文件,不添加会报错的哦、

5.AndroidManifest注册文件里面添加一些需要的应用权限和注册receiver、

 <!-- Required -->
    <permission 
        android:name="您应用的包名.permission.JPUSH_MESSAGE"  
        android:protectionLevel="signature" />

    <!-- Required -->
    <uses-permission android:name="您应用的包名.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里面添加相应的代码和Appkey,Appkey就是注册完应用的那个key值、
 
<!-- 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="您应用的包名"/> 
</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="您应用的包名"/>
 </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="您应用的包名" /> 
</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" />

 
<!-- User defined. 用户自定义的广播接收器-->
 <receiver android:name="您自己定义的Receiver"
 android:enabled="true">
 <intent-filter> 
<!--Required 用户注册SDK的intent--> 
<action android:name="cn.jpush.android.intent.REGISTRATION" /> 
<!--Required 用户接收SDK消息的intent--> 
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
 <!--Required 用户接收SDK通知栏信息的intent-->
 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> 
<!--Required 用户打开自定义通知栏的intent--> 
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
 <!-- 接收网络变化 连接/断开 since 1.6.3 --> 
<action android:name="cn.jpush.android.intent.CONNECTION" />
 <category android:name="您应用的包名" />
 </intent-filter> 
</receiver>

 

<!-- 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"/> 

6.在application的onCreate方法中添加初始化代码、

//极光推送 JPushInterface.setDebugMode(true); JPushInterface.init(this);

7.自定义一个消息接收器MyReceiver,里面可都有注释我就不多说了、

package 和易生活;

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

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

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * 如果不定义这个 Receiver,则跳转默认用户会打开主界面,接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.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);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
            String hqfjzd = bundle.getString(JPushInterface.EXTRA_EXTRA);//接收附加字段
            JSONObject object = null;
            try {
                object = new JSONObject(hqfjzd);
                String cllhqfj = object.getString("Article");//获取附加字段
                //根据附加字段的值判断跳相应的界面
                if (cllhqfj.equals("1")) {
                    //通知栏跳转APP消息界面
                    Intent i = new Intent(context, ReceiverCS.class);
                    i.putExtras(bundle);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                } else {
                    //通知栏跳转html网页
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.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);
            Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    // 打印所有的 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))) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Log.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }
}

补充一点就是设置标签

Set<String> set = new HashSet<>();//名字任意,可多添加几个
set.add(jo.getString("sex"));
set.add(jo.getString("sheng"));
set.add(jo.getString("shi"));
set.add(jo.getString("qu"));
JPushInterface.setTags(LoginAndRegisterActivity.this, set, null);//设置标签

----------补充极光推送自定义样式和铃声等,铃声最好是mp3格式,放在res下raw文件中,这个自定义的方法应该放在接收自定义消息那里,但是我通知需要自定义的铃声,所以我放在了接收通知这里、

/**
 * 自定义消息样式,铃声
 *
 * @param context
 * @param bundle
 */
private void processCustomMessage(Context context, Bundle bundle) {
    NotificationManager manger = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //为了版本兼容  选择V7包下的NotificationCompat进行构造
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    //Ticker是状态栏显示的提示
    builder.setTicker(bundle.getString(JPushInterface.EXTRA_TITLE));
    //第一行内容  通常作为通知栏标题
    builder.setContentTitle(bundle.getString(JPushInterface.EXTRA_TITLE));
    //第二行内容 通常是通知正文
    builder.setContentText(bundle.getString(JPushInterface.EXTRA_MESSAGE));
    //可以点击通知栏的删除按钮删除
    builder.setAutoCancel(true);
    //系统状态栏显示的小图标
    builder.setSmallIcon(R.mipmap.logo_new);
    Notification notification = builder.build();
    notification.sound = Uri.parse("android.resource://"
            + context.getPackageName() + "/" + R.raw.dingdan);
    builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_LIGHTS);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    Intent clickIntent = new Intent(); //点击通知之后要发送的广播
    int id = (int) (System.currentTimeMillis() / 1000);
    clickIntent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED);
    clickIntent.putExtra(JPushInterface.EXTRA_EXTRA, bundle.getString(JPushInterface.EXTRA_EXTRA));
    PendingIntent contentIntent = PendingIntent.getBroadcast(context, id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.contentIntent = contentIntent;
    manger.notify(id, notification);
}

只播放语音形式

MediaPlayer mediaPlayer;
 /**
     * 新订单语音提示
     *
     * @param
     */
    public void newshowSound() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();//初始化mediaPlayer
        }
        mediaPlayer.reset();//重置mediaPlayer播放状态为初始状态
        mediaPlayer = MediaPlayer.create(mContext, R.raw.newdd);//装载需要播放的音频文件
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.start();
        mediaPlayer.setVolume(1f, 1f);
    }

--------------退出极光和恢复极光,一般在退出APP的时候退出极光,启动APP的时候恢复极光

//启动极光
JPushInterface.resumePush(getApplicationContext());
//退出极光
JPushInterface.stopPush(MianHomePage.this);

 

 

--------------这样就OK了,运行APP然后去发送消息看看是否成功、

 

 

 

 

 

------------完美结束--------------

 

 

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶已初秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值