[解析] [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 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...