[解析] [Android]如何在应用运行时抑制推送通知被显示?

前端之家收集整理的这篇文章主要介绍了[解析] [Android]如何在应用运行时抑制推送通知被显示?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要通过两种方式处理推送通知

1)当应用程序在后台时,接收并通知我的Android客户端的状态栏;

2)当我的应用程序在前台时,收到并处理通知而不在状态栏上显示;

对于(1)很简单,我把

并打电话
PushService.setDefaultPushCallback(context,classObject);
并且通知正确显示在状态栏上.

我的问题是(2):

>我尝试创建一个自定义的BroadCastReceiver,但解析将通知发送到我之前,并将其显示在状态栏上;
我试图关掉
PushService.setDefaultPushCallback(context,classObject)
onStart方法通过设置classObject的null值,但是当我这样做的时候,我的接收者从来没有被调用,通知没有出现;

在解析之前是否有任何可以拦截通知,还是有其他可以解决问题的事情?

ps:我需要从服务器发送带有“alert”的消息

韩国社交协会,

解决方法

如果您在json数据中使用“alert”或“title”,则com.parse.PushService将拦截显示标准通知.

相反,创建您自己的BroadCastReceiver并发送标题,例如. json中的“header”.然后,您可以在onReceive处理程序中控制何时何地显示.

例如

  1. public class MyBroadcastReceiver extends BroadcastReceiver {
  2. private static final String TAG = "MyBroadcastReceiver";
  3.  
  4. @Override
  5. public void onReceive(Context context,Intent intent) {
  6. try {
  7. String action = intent.getAction();
  8. String channel = intent.getExtras().getString("com.parse.Channel");
  9. JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
  10. String title = "New alert!";
  11. if (json.has("header"))
  12. title = json.getString("header");
  13. generateNotification(context,getImg(),title);
  14. } catch (Exception e) {
  15. Log.d(TAG,"JSONException: " + e.getMessage());
  16. }
  17. }
  18.  
  19. public static void generateNotification(Context context,int icon,String message) {
  20. // Show the notification
  21. long when = System.currentTimeMillis();
  22. NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
  23. Notification notification = new Notification(icon,message,when);
  24. String title = context.getString(R.string.app_name);
  25. Intent notificationIntent = new Intent(context,SnapClientActivity.class);
  26.  
  27. // set intent so it does not start a new activity
  28. notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  29. PendingIntent intent = PendingIntent.getActivity(context,notificationIntent,0);
  30. notification.setLatestEventInfo(context,title,intent);
  31. notification.vibrate = new long[] { 500,500 };
  32. notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  33.  
  34. notification.flags =
  35. Notification.FLAG_AUTO_CANCEL |
  36. Notification.FLAG_SHOW_LIGHTS;
  37.  
  38. notificationManager.notify(0,notification);
  39. }
  40. }

猜你在找的Android相关文章