迅速集成Jpush极光推送功能

                                              迅速集成Jpush极光推送功能

一集成
   1下载官方集成SDk   https://www.jiguang.cn/push    后解压jpush-android-release-3.0.6
    2复制libs中的 jcore-android_v1.1.3.jar  和jpush-android_v3.0.6.jar到工程的libs中        并添加上依赖
    3新建src/main里创建存放cpu的文件夹  jniLibs    将sdk中的文件复制到此目录    并在app下的build Gradle里添加如下代码指向该资源
android {
    ...
    sourceSets.main{
        jniLibs.srcDir 'src/main/jniLibs'
    }
}
     4清单文件  根据SDk中的 AndroidManifest.xml     复制required的部分  到清单文件中
         将标注为“您的包名”  替换成app的包名                               将标注为您应用的appKey替换为在极光注册该应用时的appKey
    5在自己应用的application的onCreat()的方法里初始化极光推送         
        
public void onCreate() {    	     
    	 Logger.d(TAG, "[ExampleApplication] onCreate");
         super.onCreate();
         JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
         JPushInterface.init(this);     		// 初始化 JPush
    }
     6自定义推送消息的广播接受者  来处理各种推送下来的广播消息 与通知等等
   
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            //接受到推送过来的消息
            Bundle bundle = intent.getExtras();
            Logger.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

            /**
             *    Logger.d(TAG, "JPush用户注册成功");
             */
            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);



                /**
                 * [MyReceiver] 接收到推送下来的通知
                  */
            } 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);
                /**
                 *    点开通知做的处理   可以跳转到自己的activity
                 */
            } 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);


                BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(context);
                builder.statusBarDrawable = R.drawable.jpush_notification_icon;
                builder.notificationFlags = Notification.FLAG_AUTO_CANCEL
                        | Notification.FLAG_SHOW_LIGHTS;  //设置为自动消失和呼吸灯闪烁
                builder.notificationDefaults = Notification.DEFAULT_SOUND
                        | Notification.DEFAULT_VIBRATE
                        | Notification.DEFAULT_LIGHTS;  // 设置为铃声、震动、呼吸灯闪烁都要
                JPushInterface.setPushNotificationBuilder(1, builder);
                /**
                 *   推送富媒体的
                  */
            } 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) {

        }

    }
}
       7运行后在控制台上就可以推送消息了
二集成时可能出现的问题
   E/JPush: [JPushGlobal] Get sdk version fail![获取sdk版本失败!]
   E/JPush: [JPushGlobal] JPush .so file do not match JPush .jar file in the project, Failed to init JPush
   解决:
  

二 标签和别名的使用
    1为什么使用标签和别名
    推送消息时,要指定推送的对象:全部,某一个人,或者某一群人。
     2工作方式
    客户端开发者App调用 setAliasAndTags API 来设置关系
    JPush SDK 把该关系设置保存到 JPush Server 上
    在服务器端推送消息时,指定向之前设置过的别名或者标签推送
   3别名

    alias可能是一对一   ,推荐一对一单独推送,

    举例:在一个用户要登录的游戏中,可能设置别名为 userid。游戏运营时,发现该用户 3 天没有玩游戏了,则根据 userid调用服务器端API发 通知到客户端提醒用户。

    也可能是一对多

     如果多个人使用了同一个别名,,向这个别名推送消息时,带有这个别名的所有用户都会收到消息。

    4标签

     为安装了应用程序的用户,打上标签。其目的主要是方便开发者根据标签,来批量下发 Push消息。可为每个用户打多个标签。

     举例: game, old_page, women

    4设置别名与标签   一般在登录界面设置     结合登录名    用户名id    域id     组织架构id等等

      JPushInterface.setAliasAndTags(getApplicationContext(),

             imModule.getAlias(), //一个字符串

             imModule.getTags(),  //一个set集合标签集合

              mAliasCallback);    //设置的回调接口

    设置别名和标签

     

        Set<String> set = new HashSet<>();
        set.add("@" + user.getDomainID());
        set.add(user.getOrg_id() + "@" + user.getDomainID());
        ImModule imModule = new ImModule();
        imModule.setAlias(alias + "@" + user.getDomainID());
        imModule.setTags(set);
        // 调用 Handler 来异步设置别名
//        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alias));
        message = mHandler.obtainMessage();
        message.what = MSG_SET_ALIAS;
        message.obj = imModule;
        mHandler.sendMessage(message);

     接受到消息开始设置标签和别名

    

  ImModule imModule = (ImModule) msg.obj;
                        // 调用 JPush 接口来设置别名。
                        JPushInterface.setAliasAndTags(getApplicationContext(),
                                imModule.getAlias(),
                                imModule.getTags(),
                                mAliasCallback);


     添加设置回调  当设置失败后重试

    

private final TagAliasCallback  mAliasCallback = new TagAliasCallback() {
     @Override
     public void gotResult(int code, String alias, Set<String> tags) {
	String logs;
	switch (code) {
	    case 0:
	        logs = "Set tag and alias success";
	        Logger.i(TAG, logs);
                break;
	    case 6002:
	        logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
		Logger.i(TAG, logs);
                //如果联网  就60秒后重试
		if (ExampleUtil.isConnected(getApplicationContext())) {
	  mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
		} else {
		   Logger.i(TAG, "No network");
		}
		break;

	     default:
		logs = "Failed with errorCode = " + code;
		Logger.e(TAG, logs);
		}

	ExampleUtil.showToast(logs, getApplicationContext());
     }

};


    三定制通知和消息的样式

    1 定制简单的通知样式  使用BasicPushNotificationBuilder

    

BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(MainActivity.this);
builder.statusBarDrawable = R.drawable.jpush_notification_icon;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL
        | Notification.FLAG_SHOW_LIGHTS;  //设置为自动消失和呼吸灯闪烁
builder.notificationDefaults = Notification.DEFAULT_SOUND
        | Notification.DEFAULT_VIBRATE
        | Notification.DEFAULT_LIGHTS;  // 设置为铃声、震动、呼吸灯闪烁都要
参数一     为某种类型的 PushNotificationBuilder 指定编号。
JPushInterface.setPushNotificationBuilder(1, builder);

    2 定制带按钮的Notification样式   MultiActionsNotificationBuilder

MultiActionsNotificationBuilder builder = new MultiActionsNotificationBuilder(PushSetActivity.this);
//添加按钮,参数(按钮图片、按钮文字、扩展数据)
builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "first", "my_extra1");
builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "second", "my_extra2");
builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "third", "my_extra3");
JPushInterface.setPushNotificationBuilder(2, builder)


    3高级定制      使用CustomPushNotificationBuilder   定义通知里的布局

   

CustomPushNotificationBuilder builder = new
    CustomPushNotificationBuilder(MainActivity.this,
                              R.layout.customer_notitfication_layout,
                              R.id.icon,
                              R.id.title,
                              R.id.text); 
                             // 指定定制的 Notification Layout
    builder.statusBarDrawable = R.drawable.your_notification_icon;     
    // 指定最顶层状态栏小图标
    builder.layoutIconDrawable = R.drawable.your_2_notification_icon;  
    // 指定下拉状态栏时显示的通知图标
    JPushInterface.setPushNotificationBuilder(2, builder);


    4终极设定  自定义通知   不使用Jpush里的   App取到自定义消息全部内容,然后App自己来写代码做通知的




 





      






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值