[android推送系统升级]Android极光推送的例子

更新时间:2020-11-25    来源:.Net开发    手机版     字体:

【www.bbyears.com--.Net开发】

一、集成SDK(这里推荐自动集成)

1.确认android studio的 Project 根目录的主 gradle 中配置了jcenter支持。(一般默认支持)

buildscript {
    repositories {
        jcenter()
    }
    ...
}
 
allprojects {
    repositories {
        jcenter()
    }
}

2.在 module 的 gradle 中添加依赖和AndroidManifest的替换变量


android {
    ...
    defaultConfig {
        applicationId "xx.xxx.xxxx"<code>//JPush上注册的包名
       ...
 
        ndk {
            //选择要添加的对应cpu类型的.so库。
            abiFilters "armeabi", "armeabi-v7a", "armeabi-v8a","x86", "x86_64", "mips", "mips64"
        }
 
        manifestPlaceholders = [
            JPUSH_PKGNAME : applicationId,
            JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
            JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
        ]
        ...
    }
    ...
}
 
dependencies {
    compile "cn.jiguang:jpush:2.2.0"
    ...
}
 

二、在AndroidManifest中配置权限


 
 
 
 
 
 
 
 
 
 
 
 

三、代码

新建一个Application,并添加一下代码

@Override
public void onCreate() {
 super.onCreate();
 JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
 JPushInterface.init(this); // 初始化 JPush
}
说明:

1.init 只需要在应用程序启动时调用一次该 API 即可

2.以上 Application 类。需要在 AndoridManifest.xml 里配置

 

到此,极光推送的基本功能已经完成,推送通知后应用程序会接收到通知,通知默认图标为应用程序图标,点击通知会打开程序首页。

但是,当你的应用程序是打开状态时,点击通知后会重新运行一次程序并打开首页,显然这不是我们想要的,我们还需要配置接受到通知之后的响应动作。

 
四、自定义一个接收器


/**
 * 自定义接收器
 *
 * 如果不定义这个 Receiver,则:
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";
 
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();//接收到的消息
 
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...
 
        } 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] 用户点击打开了通知");
 
            //打开自定义的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())) {
            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());
        }
    }
 
 
}

并在 AndoridManifest.xml 中配置



    android:name="cn.jpush.android.service.PushReceiver"
    android:enabled="true"
    android:exported="false">
   
          
       
       
   

   
       
       
   

   
   
       
       
       
   

 

 



    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true">
   
       
       
       
       
       
       
       
   


本文来源:http://www.bbyears.com/asp/112504.html

热门标签

更多>>

本类排行