1.需要先在极光推送官网注册应用,得到appkey.然后将libs中的jar放入
2.然后在项目首页加入推送
将此段代码放置到首次加载的界面中
3. 在onCreate中,放入以下两行代码进行注册.
4.copy代码,一行不改
5.在项目合适的地方,例如得到了user_id的地方,去设置tag和alisa. 调用PushUtils里面的两个方法,
setTag和setAlias.
6.还需要把 官方Demo中的 MyReceiver放入项目中.在第45行左右.加入点击通知后要打开的activity.
7.最后需要在AndroidMainfest.xml中加入最后的代码
看完了么,,,看完了吧,其实在打开极光推送官网中,选择好自己的应用,打开应用详情,android里面有一个快速集成,下载了之后,AndroidMainfest.xml,res,libs等文件全部有了,只需要copy进去,改一下 A ndroidMainfest.xml中的一些包名,appkey就可以.
2.然后在项目首页加入推送
//*********************推送**********************
public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
public static final String KEY_TITLE = "title";
public static final String KEY_MESSAGE = "message";
public static final String KEY_EXTRAS = "extras";
public static boolean isForeground = false;
private MessageReceiver mMessageReceiver;
public void registerMessageReceiver() {
mMessageReceiver = new MessageReceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(MESSAGE_RECEIVED_ACTION);
registerReceiver(mMessageReceiver, filter);
}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
String messge = intent.getStringExtra(KEY_MESSAGE);
String extras = intent.getStringExtra(KEY_EXTRAS);
StringBuilder showMsg = new StringBuilder();
showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
if (!ExampleUtil.isEmpty(extras)) {
showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
}
setCostomMsg(showMsg.toString());
}
}
}
private void setCostomMsg(String msg){
//if (null != msgText) {
//msgText.setText(msg);
//msgText.setVisibility(android.view.View.VISIBLE);
//}
}
3. 在onCreate中,放入以下两行代码进行注册.
JPushInterface.init(getApplicationContext());
registerMessageReceiver();
4.copy代码,一行不改
@Override
protected void onResume() {
super.onResume();
JPushInterface.onResume(WelcomeActivity.this);
isForeground = true;
}
@Override
protected void onPause() {
isForeground = false;
JPushInterface.onPause(WelcomeActivity.this);
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
6.还需要把 官方Demo中的 MyReceiver放入项目中.在第45行左右.加入点击通知后要打开的activity.
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
Intent i = new Intent(context, MainActivity.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
context.startActivity(i);
7.最后需要在AndroidMainfest.xml中加入最后的代码
<!-- Required SDK核心功能 -->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.arkunion.jiazheng" /><!-- 自己项目的包名 -->
</intent-filter>
</activity>
<!-- Required SDK核心功能 -->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false" >
</service>
<!-- 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="com.arkunion.jiazheng" /><!-- 自己项目的包名 -->
</intent-filter>
</service>
<!-- Required SDK核心功能 -->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter android:priority="1000" >
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!-- Required 显示通知栏 -->
<category android:name="com.arkunion.jiazheng" /><!-- 自己项目的包名 -->
</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核心功能 -->
<receiver
android:name="cn.jpush.android.service.AlarmReceiver"
android:exported="false" />
<!-- User defined. For test only 用户自定义的广播接收器 -->
<receiver
android:name="com.arkunion.jiazheng.push.MyReceiver"<!-- 自己项目的包名 -->
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required 用户注册SDK的intent -->
<action android:name="cn.jpush.android.intent.UNREGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required 用户接收SDK消息的intent -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required 用户接收SDK通知栏信息的intent -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required 用户打开自定义通知栏的intent -->
<action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->
<action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->
<category android:name="com.arkunion.jiazheng" /><!-- 自己项目的包名 -->
</intent-filter>
</receiver>
<!-- Required . Enable it you can get statistics data with channel -->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<meta-data
android:name="JPUSH_APPKEY"
android:value="" /><!-- </>值来自开发者平台取得的AppKey -->
看完了么,,,看完了吧,其实在打开极光推送官网中,选择好自己的应用,打开应用详情,android里面有一个快速集成,下载了之后,AndroidMainfest.xml,res,libs等文件全部有了,只需要copy进去,改一下 A ndroidMainfest.xml中的一些包名,appkey就可以.