标签:
推送即服务器给客户端发送通知消息
<?xml version="1.0" encoding="utf-8"?> <manifest package="org.mobiletrain.a9_1jpush" xmlns:android="http://schemas.android.com/apk/res/android"> <permission android:name="${applicationId}.permission.JPUSH_MESSAGE" android:protectionLevel="signature"/> <!-- Required 一些系统要求的权限,如访问网络等 --> <uses-permission android:name="${applicationId}.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"/> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- Rich push 核心功能 since 2.0.6 --> <activity android:name="cn.jpush.android.ui.PopWinActivity" android:exported="false" android:theme="@style/MyDialogStyle"> </activity> <!-- 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="${applicationId}"/> </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> <!-- 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="${applicationId}"/> </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"/> <!-- 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="82426619de889cf8b2bea0e5"/> <!-- </>值来自开发者平台取得的AppKey --> <receiver android:name=".MyReceiver"> <intent-filter> <!-- 监听用户点击通知栏事件 --> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED"/> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <category android:name="org.mobiletrain.a9_1jpush"/> </intent-filter> </receiver> <activity android:name=".Main2Activity"> </activity> </application> </manifest>
public class MyReceiver extends BroadcastReceiver { //因为广播是在清单文件中注册的,所以这里的Context是Application的Context,Application的Context不能直接用来启动一个 //Activity //如果广播是在Activity中注册的,则广播中的Context是Activity的Context,可以直接用来启动新的Activity @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); // Log.d(TAG, "onReceive - " + intent.getAction()); Log.d("google_lenve_fb", "onReceive: " + intent.getAction()); //请求成功后获得注册ID if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { //当收到消息时触发 } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { // System.out.println("收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE)); Log.d("google_lenve_fb", "onReceive: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); Log.d("google_lenve_fb", "onReceive: " + bundle.getString(JPushInterface.EXTRA_EXTRA)); // 自定义消息不会展示在通知栏,完全要开发者写代码去处理 } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { // System.out.println("收到了通知"); Log.d("google_lenve_fb", "onReceive: 收到通知"); // 在这里可以做些统计,或者做些其他工作 //如果用户在点击通知栏 } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Intent intent1 = new Intent(context, Main2Activity.class); intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); } else { Log.d("google_lenve_fb", "onReceive: ========="); } } }
** * Created by wangsong on 2016/5/9. * REGISTRATION_ID 设备的唯一标识符 * 别名 :用户的唯一标识符 * 标签:用户可以有多个标签 * <p/> * 错误:指定的接收者目前还没有任何设备进行注册或者设置 * 1.是否在MyApplication类中初始化了JPush * 2.是否在Application节点中添加了name属性 * 3.检查AppKey是否正确 * 4.检查清单文件是否正确合并 */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // JPushInterface.init(this); //给不同的用户设置一个别名,一个用户只能设置一个别名 // JPushInterface.setAlias(this, "qq", null); //给不同的用户设置标签,一个用户可以设置多个标签 // Set<String> set = new HashSet<>(); // set.add("wx"); // set.add("wb"); // set.add("qq"); // set.add("微博"); // JPushInterface.setTags(this, set, null); } }
标签:
原文地址:http://www.cnblogs.com/anni-qianqian/p/5473490.html