标签:
1.到极光推送官网注册用户。
2.创建一个应用根据包名申请app key,注意一个包名对应一个appkey,如果不对应则不能实现功能。
3.下载官网上的SDK,内附指南。
4.首先是各种粘贴复制,注意别漏掉
5.然后是配置Manifest.xml文件
1)一开始第一条permission被我忘了
2)除了标注着required的部分还要把MyReceiver的部分也复制过来
3)注意该修改包名的地方和最后的appkey,不过官网提供的example里已经是改好的了可以直接用
<permission android:name="com.zhangkong.module.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> <!-- Required 一些系统要求的权限,如访问网络等--> <uses-permission android:name="com.zhangkong.module.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.WRITE_SETTINGS" /> <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.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <!-- Optional for location --> <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" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:name="com.zhangkong.module.MyApplication"> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Interface1" android:label="@string/app_name" > </activity> <activity android:name=".Interface3" android:label="@string/app_name" > </activity> <!-- Rich push 核心功能 since 2.0.6--> <activity android:name="cn.jpush.android.ui.PopWinActivity" android:theme="@style/MyDialogStyle" android:exported="false"> </activity> <!-- 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="com.zhangkong.module" /> </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.zhangkong.module" /> </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.zhangkong.module" /> </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.zhangkong.module.MyReceiver" android:exported="false" android:enabled="true"> <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.zhangkong.module" /> </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="ea3709c8dac9957b7aa36ba1" /> <!-- </>值来自开发者平台取得的AppKey--> </application>
6.需要复制过来三个类
7.其他两个都不需要改动,关键的在MyReceiver类里,和在自己的MainActivity里添加相应的代码
//send msg to MainActivity private void processCustomMessage(Context context, Bundle bundle) { if (MainActivity.isForeground) { String message = bundle.getString(JPushInterface.EXTRA_MESSAGE); String extras = bundle.getString(JPushInterface.EXTRA_EXTRA); Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);//这个activity也要改 msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);//注意这个activity要改成自己的主界面,需要自己定义 if (!ExampleUtil.isEmpty(extras)) { try { JSONObject extraJson = new JSONObject(extras); if (null != extraJson && extraJson.length() > 0) { msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);//这里同理 } } catch (JSONException e) { } } context.sendBroadcast(msgIntent); } }8.在自己的MainActivity中添加代码,可以直接从example的MainActivity中复制过来,红色的部分是我自己加上的,不加总是收不到推送,从网上看到一个例子说需要加上。
public class MainActivity extends Activity { private Button button1; private Button
public static boolean isForeground = false; private EditText msgText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); JPushInterface.init(this); registerMessageReceiver(); // used for receive msg init(); button1 = (Button) this.findViewById(R.id.button1); button3 = (Button) this.findViewById(R.id.button3); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Interface1.class); startActivity(intent); } }); button3.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Interface3.class); startActivity(intent); } }); } private void init() { JPushInterface.init(getApplicationContext()); } @Override protected void onResume() { isForeground = true; super.onResume(); JPushInterface.onResume(this);//这句是自己加上的 } @Override protected void onPause() { isForeground = false; super.onPause(); JPushInterface.onPause(this); } @Override protected void onDestroy() { unregisterReceiver(mMessageReceiver); super.onDestroy(); } public static final String KEY_TITLE = "title"; public static final String KEY_MESSAGE = "message"; public static final String KEY_EXTRAS = "extras"; private MessageReceiver mMessageReceiver; public static final String MESSAGE_RECEIVED_ACTION = "com.zhangkong.module.MESSAGE_RECEIVED_ACTION";//记得修改包名 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); } } }9.然后就全部OK了,到官网推送个消息试。点击消息后有一个默认显示的界面,不知道为什么和我预设的不对,下次再看看,解决后再写吧!
标签:
原文地址:http://blog.csdn.net/ulike_mfy/article/details/51992262