极光推送:eclipse手动集成步骤

以下仅作为自己的工作笔记。

// 此处以JPush 3.0.9 版本为例。
// 此处以JCore 1.1.7 版本为例。

1.将下载下来的官方文档sdk解压,然后复制jar包和.so文件及drawable-hdpi, layout, values文件夹中的资源文件。

1.1 。 将libs中jcore-android-1.1.7.jar和jpush-android-3.0.9.jar包及所有.so文件复制粘贴至主工程的libs中。(记得将jar包Build Path)

sdk    libs中jar包及.so文件

复制粘贴后的主工程libs

1.2.将res/ 中drawable-hdpi,layout, values文件夹中的资源文件全部复制至到你的主工程中 res/ 对应同名的目录下。

2.根据 SDK 压缩包里的 AndroidManifest.xml 样例文件,来配置你的应用程序项目的 AndroidManifest.xml 。

2.1  将如下权限添加到自己的AndroidManifest.xml中

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

<!-- Optional. Required for location feature -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 -->
<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" />
<uses-permission android:name="android.permission.GET_TASKS" />

2.2 将如下核心功能添加至你的application中。
< application
          android:name = "自己的 Application名字 "
          android:allowBackup = "true"
          android:icon = "@drawable/ic_caijing"
          android:label = "@string/app_name"
          android:theme = "@style/AppTheme"   >

<!-- 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 3.0.9 Required SDK 核心功能-->
<provider
android:authorities="您应用的包名.DataProvider"
android:name="cn.jpush.android.service.DataProvider"
android:exported="true"
/>

<!-- 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>
<!-- SDK核心功能-->
<activity
android:name="cn.jpush.android.ui.PopWinActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@style/MyDialogStyle">
<intent-filter>
<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"/>


  </ application >





3.初始化SDK

public   class   MyAplication   extends   Application {
     
          // private static ACache mCache;
      @Override
      public   void   onCreate() {
            //   TODO   Auto-generated method stub
            super .onCreate();
        
          // Jpush 初始化
        JPushInterface. setDebugMode ( true );
        JPushInterface. init ( this );
       
      
     }
     

}

4.设置标签与别名(在登录成功后调用如下方法)

4.1
    /**
     * 设置标签与别名
     */
      @SuppressWarnings ( "deprecation" )
      private   void  setTagAndAlias() {
            //  TODO  Auto-generated method stub
                 /**
         *这里设置了别名,在这里获取的用户登录的信息
         *并且此时已经获取了用户的userId,然后就可以用用户的userId来设置别名了
         **/
         Set<String> tags =  new  HashSet<String>();
         //这里可以设置你要推送的人,一般是用户 uid  不为空在设置进去 可同时添加多个
         if  (!TextUtils. isEmpty ( userId )){
           tags.add( userId ); //设置tag
           }
         //上下文、别名【Sting行】、标签【Set型】、回调
        JPushInterface. setAliasAndTags ( context ,  userId , tags,
                 mAliasCallback );
     
     }

4.2
/**
      * 设置标签与别名的回调
      */
      @SuppressWarnings ( "deprecation" )
      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极光推送别名设置成功" ;
                    Log. e ( "TAG" , logs);
                     break ;
                 case  6002:
               
                    logs =  "Failed to set alias and tags due to timeout. Try again after 60s.极光推送别名设置失败,60秒后重试" ;
                    Log. e ( "TAG" , logs);
                     break ;
                 default :
                    logs =  "极光推送设置失败,Failed with errorCode = "  + code;
                    Log. e ( "TAG" , logs);
                     break ;
            }
        }
    };

4.3(当用户退出登入时,调用如下方法

/**
     * 取消设置标签与别名
     */
      @SuppressWarnings ( "deprecation" )
      private   void  cancleTagAndAlias() {
            //  TODO  Auto-generated method stub
            // TODO  上下文、别名、标签、回调  退出后空数组与空字符串取消之前的设置
        Set<String> tags =  new  HashSet<String>();
        JPushInterface. setAliasAndTags ( context ,  "" , tags,  mAliasCallback );
           
     }



5.用户自定义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);
            receivingNotification(context,bundle);
        }  else   if  (JPushInterface. ACTION_NOTIFICATION_OPENED .equals(intent.getAction())) {
            Log. d ( TAG ,  "[MyReceiver] 用户点击打开了通知" );
             openNotification(context,bundle);
           
        }  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  {
           Log. d ( TAG ,  "[MyReceiver] Unhandled intent - "  + intent.getAction());
        }
     }
      // 打印所有的 intent extra 数据
      @SuppressLint ( "NewApi" )
      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_EXTRA )) {
                      if  (bundle.getString(JPushInterface. EXTRA_EXTRA ).isEmpty()) {
                           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();
     }
     
     
       private   void  receivingNotification(Context context, Bundle bundle){
             String title = bundle.getString(JPushInterface. EXTRA_NOTIFICATION_TITLE );
             Log. i ( TAG ,  " title : "  + title);
             String message = bundle.getString(JPushInterface. EXTRA_ALERT );
             Log. i ( TAG ,  "message : "  + message);
             String extras = bundle.getString(JPushInterface. EXTRA_EXTRA );
             Log. i ( TAG ,  "extras : "  + extras);
          
         }
     
     
       private   void  openNotification(Context context, Bundle bundle){
             String extras = bundle.getString(JPushInterface. EXTRA_EXTRA );
             String myValue =  "" ;
              try  {
                 JSONObject extrasJson =  new  JSONObject(extras);
                  //得到后台给的参数所对应的值。
                 myValue = extrasJson.optString( "extraParm" );
                 Log. i ( "extraParm---" , myValue);
             }  catch  (Exception e) {
                Log. w ( TAG ,  "Unexpected: extras is not a valid json" , e);
                  return ;
             }
              //根据值判断跳转页面
              if  ( "1" .equals(myValue)) {
                 Intent mIntent =  new  Intent(context, StuSleepManagementActivity. class );
                 mIntent.putExtras(bundle);
                 mIntent.setFlags(Intent. FLAG_ACTIVITY_NEW_TASK );
                 context.startActivity(mIntent);
             }
              else   if  ( "2" .equals(myValue)){
                 Intent mIntent =  new  Intent(context, NewSleepFacultyQueryActivity. class );
                 mIntent.putExtras(bundle);
                 mIntent.setFlags(Intent. FLAG_ACTIVITY_NEW_TASK );
                 context.startActivity(mIntent);
             }
         }
}


猜你喜欢

转载自blog.csdn.net/lumingzhang/article/details/79641717