[Parse] [Android]如何在应用运行时禁止显示推送通知?

use*_*863 9 android broadcastreceiver push-notification parse-platform

我需要通过两种方式处理推送通知:

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

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

对于(1)非常简单,我把它调用了PushService.setDefaultPushCallback(context,classObject); 并且通知正确显示在状态栏上.

我的问题是(2):

  • 我尝试创建一个自定义的BroadCastReceiver,但是解析会在我之前收到通知并在状态栏上显示;
  • 我试图通过为classObject设置null值来关闭OnStart方法上的PushService.setDefaultPushCallback(context,classObject),但是当我这样做时,我的接收器从未被调用过,并且没有出现通知;

在解析之前我有什么办法可以拦截通知,还是我可以做另外一件事来解决我的问题?

ps:我需要通过"alert"从服务器发送消息

韩国社交协会,

小智 28

如果在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, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, 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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Ign*_*oda 7

我有同样的问题.在尝试了不成功的其他解决方案之后,我尝试了更简单的解决方案......并且它起作用了.

使用自定义ParsePushBroadcastReceiver时,请覆盖onPushReceiver方法.并且只有在您的应用处于非活动状态时才调用超类方法.

public class ParsePushReceiver extends com.parse.ParsePushBroadcastReceiver{

    @Override
    protected void onPushReceive(Context context, Intent intent ) {
        // do your stuff here
        if( !MyApp.active )
            super.onPushReceive(context, intent );
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这真是一个优雅的解决方案,以防万一其他人想知道你必须更换接收器名称<receiver android:name ="com.parse.ParsePushBroadcastReceiver"android:exported ="false"> <intent-filter> <action android :name ="com.parse.push.intent.RECEIVE"/> <action android:name ="com.parse.push.intent.DELETE"/> <action android:name ="com.parse.push.intent.在这里用自定义类名打开"/> </ intent-filter> </ receiver>它会ParsePushReciever (2认同)