进行到这里就可以接收到通知了,但是如果你还想根据接收的消息做点什么

step8:
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver"; private NotificationManager nm; @Override
public void onReceive(Context context, Intent intent) {
if (null == nm) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
} Bundle bundle = intent.getExtras();
// Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
Log.d(TAG, "JPush 用户注册成功"); } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的自定义消息"); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的通知"); receivingNotification(context,bundle); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "用户点击打开了通知"); openNotification(context,bundle); } else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
} private void receivingNotification(Context context, Bundle bundle){
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
Log.d(TAG, " title : " + title);
String message = bundle.getString(JPushInterface.EXTRA_ALERT);
Log.d(TAG, "message : " + message);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Log.d(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("myKey");
} catch (Exception e) {
Log.w(TAG, "Unexpected: extras is not a valid json", e);
return;
}
Log.d(TAG, "myValue : " + myValue);
if (TYPE_THIS.equals(myValue)) {
Intent mIntent = new Intent(context, ThisActivity.class);
mIntent.putExtras(bundle);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
} else if (TYPE_ANOTHER.equals(myValue)){
Intent mIntent = new Intent(context, AnotherActivity.class);
mIntent.putExtras(bundle);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
}

step9:最后别忘了在清单中注册

    <!--极光推送接收消息-->
<receiver
android:name="com.psm.admininstrator.lele8teach.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.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.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 -->
<category android:name="com.psm.admininstrator.lele8teach" />
</intent-filter>
</receiver>
</application>

原博客地址:https://blog.csdn.net/jinjianghai/article/details/80336099

最新文章

  1. Dotnet文件格式解析
  2. [HDOJ5943]Kingdom of Obsession(最大匹配,思路)
  3. Shell教程2-变量
  4. XCode6.1中的ios7.1适配
  5. selenium自动化遇见Cannot find class in classpath问题
  6. hackerrank Ticket
  7. 【抱怨文】vscode对多项目支持不够友好
  8. datetime 计算时间差
  9. DAY16 模块和包的导入
  10. CM5.15安装kafka
  11. win10新系统修改onedrive目录,提示找不到OneDrive目录
  12. Mysql高可用
  13. 内存占用过高 kill 调整mysql内存占用
  14. 一场关于 .net core 和 .net framework 编码的案情分析
  15. UML的通用机制(三)
  16. application.properties详解 --springBoot配置文件
  17. ACM数论之旅6---数论倒数,又称逆元(我整个人都倒了( ̄﹏ ̄))
  18. noi2009变换序列
  19. 爬虫初识和request使用
  20. BZOJ 2134 单选错位 ——期望DP

热门文章

  1. vue2.0介绍
  2. 列数不固定时怎么使用el-tabel展示数据
  3. [C/C++] 深拷贝和浅拷贝
  4. sqoop工具从oracle导入数据2
  5. 做一个iframe的弹出框
  6. cf 442 D. Olya and Energy Drinks
  7. BZOJ 1098: [POI2007]办公楼biu 链表
  8. ubuntu12.04 Qt WebKit编译
  9. 【Android开发日记】之入门篇(四)——Android四大组件之Activity
  10. 搭建eova开发环境