[解析] [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处理程序中控制何时何地显示.

例如

public class MybroadcastReceiver extends broadcastReceiver {
    private static final String TAG = "MybroadcastReceiver";

    @Override
    public void onReceive(Context context,Intent intent) {
        try {
            String action = intent.getAction();
            String channel = intent.getExtras().getString("com.parse.Channel");
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            String title = "New alert!";
            if (json.has("header"))
                title = json.getString("header");
            generateNotification(context,getImg(),title);
        } catch (Exception e) {
            Log.d(TAG,"JSONException: " + e.getMessage());
        }
    }

    public static void generateNotification(Context context,int icon,String message) {
        // Show the notification
        long when = System.currentTimeMillis();
        notificationmanager notificationmanager = (notificationmanager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon,message,when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context,SnapClientActivity.class);

        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context,notificationIntent,0);
        notification.setLatestEventInfo(context,title,intent);
        notification.vibrate = new long[] { 500,500 };
        notification.sound = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);

        notification.flags = 
            Notification.FLAG_AUTO_CANCEL | 
            Notification.FLAG_SHOW_LIGHTS;

        notificationmanager.notify(0,notification);
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...