Android 10分钟集成极光推送
生活随笔
收集整理的這篇文章主要介紹了
Android 10分钟集成极光推送
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、首先申請key
https://www.jiguang.cn/accounts/login/form
2、app build.gradle添加jar依賴
?
compile 'cn.jiguang.sdk:jpush:3.0.7' compile 'cn.jiguang.sdk:jcore:1.1.3' compile 'me.leolin:ShortcutBadger:1.1.16@aar'//消息桌面顯示注意最新依賴是:
compile 'cn.jiguang.sdk:jpush:3.1.6' // 此處以JPush 3.1.6 版本為例。 compile 'cn.jiguang.sdk:jcore:1.2.5' // 此處以JCore 1.2.5 版本為例。在defaultConfig下添加key配置
manifestPlaceholders = [JPUSH_PKGNAME : applicationId,JPUSH_APPKEY : "880fe88703ab92260d7d42ca", //JPush上注冊的包名對應的appkey.JPUSH_CHANNEL : "developer-default", //暫時填寫默認值即可. ]如果項目加入了百度導航,需要在defaultConfig下添加系統配置
ndk {abiFilters "armeabi", "x86", "x86_64", "mips64", "mips" }?
3、主要receiver
?
/*** ProjectName: Myditukaifa* Author: lgq* Date: 2018/1/30 0030 14:37*/public class MyJPushReceiver extends BroadcastReceiver {private String body;private int num;// private Intent mForegroundService;private int tzid=0;@Overridepublic void onReceive(Context context, Intent intent) {try {Bundle bundle = intent.getExtras();String action = intent.getAction();Log.i("lgqq","body=====自定義消息="+action);if (action.equals("cn.jpush.android.intent.NOTIFICATION_OPENED")) {//處理點擊事件ShareUtil.sharedPint("num",0);ShortcutBadger.removeCount(context); //for 1.1.4+ 桌面信息context.startActivity(new Intent(context, ShowJpushActivity.class).putExtra("tz",bundle.getString("cn.jpush.android.ALERT")));}if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {//注冊 // Util.soutLong(TAG, "JPush用戶注冊成功");} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {// 這里會顯示極光推送默認的通知,自定義能力有限int num = ShareUtil.getSharedInt("num");num++;ShareUtil.sharedPint("num",num);ShortcutBadger.applyCount(context, num);Log.i("lgqq","body=====通知="+bundle.getString("cn.jpush.android.ALERT"));}String param = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);Log.v("lgqq","body=====action="+num+".........."+param);if (action.equals("cn.jpush.android.intent.NOTIFICATION_RECEIVED")) {//處理滑動清除和點擊刪除事件Log.v("lgqq","body=====shoudaotz=");}body = bundle.getString("cn.jpush.android.ALERT");Log.v("lgqq","body======"+body);if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);Logger.d("[MyReceiver] 接收Registration Id : " + regId);//send the Registration Id to your server... // EventBus.getDefault().post(new LoginEvent(regId));} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {Logger.d("[MyReceiver] 接收到推送下來的自定義消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));Log.i("lgqq","body=====接受到推送下來的消息=666666666666自定義消息6");tzid=tzid+1;int num = ShareUtil.getSharedInt("num");num++;ShareUtil.sharedPint("num",num);ShortcutBadger.applyCount(context, num);processCustomMessage(context, bundle);} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {Logger.d("[MyReceiver] 接收到推送下來的通知");int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);Logger.d("[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {Logger.d("[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("[MyReceiver]" + intent.getAction() +" connected state change to "+connected);} else {Logger.d("[MyReceiver] Unhandled intent - " + intent.getAction());}} catch (Exception e){} // context.startActivity(new Intent(context, TwoAcitvity.class));}//send msg to MainActivitypublic String getTime(){long currentTime = System.currentTimeMillis();SimpleDateFormat formatter = new SimpleDateFormat("HH時mm分");Date date = new Date(currentTime);System.out.println(formatter.format(date));// long time=System.currentTimeMillis()/1000;//獲取系統時間的10位的時間戳String str=String.valueOf(formatter.format(date));return str;}private void processCustomMessage(Context context, Bundle bundle) {RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.kongreveiver);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);String time = getTime(); // String hhmm = timetodate(time);customView.setTextViewText(R.id.timemy,time);if (!TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_TITLE))){customView.setTextViewText(R.id.name,bundle.getString(JPushInterface.EXTRA_TITLE));}customView.setTextViewText(R.id.neirongte,bundle.getString(JPushInterface.EXTRA_MESSAGE));Intent intentCancel = new Intent(context,NotificationBroadcastReceiver.class);PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context,0,intentCancel,PendingIntent.FLAG_ONE_SHOT);Intent companyIntroduce = new Intent(context, ShowJpushActivity.class);companyIntroduce.putExtra("tz",bundle.getString(JPushInterface.EXTRA_MESSAGE)); // companyIntroduce.putExtra("name", name);int notifyId = (int) System.currentTimeMillis();PendingIntent pendingIntent = PendingIntent.getActivity(context, notifyId, companyIntroduce, PendingIntent.FLAG_UPDATE_CURRENT);NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);mBuilder//設置通知欄標題 // .setContentText(bundle.getString(JPushInterface.EXTRA_MESSAGE)) //設置通知欄顯示內容.setContent(customView).setContentIntent(pendingIntent) //設置通知欄點擊意圖.setDeleteIntent(pendingIntentCancel)//取消消息回調 // .setTicker(context.getPackageName() + "消息")//通知首次出現在通知欄,帶上升動畫效果的.setWhen(System.currentTimeMillis())//通知產生的時間,會在通知信息里顯示,一般是系統獲取到的時間.setPriority(Notification.PRIORITY_DEFAULT) //設置該通知優先級.setAutoCancel(true)//設置這個標志當用戶單擊面板就可以讓通知將自動取消 //.setOngoing(false)//ture,設置他為一個正在進行的通知。他們通常是用來表示一個后臺任務,用戶積極參與(如播放音樂)或以某種方式正在等待,因此占用設備(如一個文件下載,同步操作,主動網絡連接)// .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加聲音、閃燈和振動效果的最簡單、最一致的方式是使用當前的用戶默認設置,使用defaults屬性,可以組合 // .setDefaults(Notification.DEFAULT_SOUND) //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加聲音 // requires VIBRATE permission.setSmallIcon(R.mipmap.mylog); // mNotificationManager.notify(notifyId, notify);Log.i("lgqq","body=====id============"+tzid+".....");int num = ShareUtil.getSharedInt("num");num++;ShareUtil.sharedPint("num",num);mNotificationManager.notify(num, mBuilder.build());}}4、次要receiver
?
/*** 自定義JPush message 接收器,包括操作tag/alias的結果返回(僅僅包含tag/alias新接口部分)* */ public class MyJPushMessageReceiver extends JPushMessageReceiver {@Overridepublic void onTagOperatorResult(Context context, JPushMessage jPushMessage) { // TagAliasOperatorHelper.getInstance().onTagOperatorResult(context,jPushMessage);Log.i("lgqq","body=====1111111=");super.onTagOperatorResult(context, jPushMessage);}@Overridepublic void onCheckTagOperatorResult(Context context,JPushMessage jPushMessage){ // TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context,jPushMessage);Log.i("lgqq","body=====2222222=");super.onCheckTagOperatorResult(context, jPushMessage);}@Overridepublic void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {Log.i("lgqq","body=====33333="); // TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context,jPushMessage);super.onAliasOperatorResult(context, jPushMessage);} }5、配置receiver
6、權限,
?
<uses-permission android:name="com.tianxin.maient.liteapp.permission.JPUSH_MESSAGE" />//自己的包名 <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>?
?
?
<receiver android:name="com.tianxin.jpushs.MyJPushReceiver"android:enabled="true"><intent-filter android:priority="1000"><action android:name="cn.jpush.android.intent.REGISTRATION" /><action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /><action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /><action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /><action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" /><action android:name="cn.jpush.android.intent.CONNECTION" /><category android:name="com.tianxin.mapclient.liteapp" />//自己的包名</intent-filter></receiver><receiver android:name="com.tianxin.jpushs.MyJPushMessageReceiver"><intent-filter><action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /><category android:name="com.tianxin.mapclient.liteapp"></category>//自己的包名</intent-filter> </receiver>7、實例化
?
JPushInterface.init(context);8、設置別名
?
JPushInterface.setAlias(this, //上下文對象JPushUtil.getDeviceId(MainActivity.this), //別名new TagAliasCallback() {//回調接口,i=0表示成功,其它設置失敗@Overridepublic void gotResult(int i, String s, Set<String> set) {Log.d("alias", "set alias result is" + i);}});9、去極光發送通知,即可收到通知
在線解決bug:qq1085220040
總結
以上是生活随笔為你收集整理的Android 10分钟集成极光推送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 图片选择器、图片剪切,文
- 下一篇: 实例分享--告诉你如何使用语音和自然语言