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

时间:2013-06-17 21:36:42

标签: android push-notification broadcastreceiver parse-platform

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

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

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

对于(1)很简单,我把它 并打电话 PushService.setDefaultPushCallback(context,classObject); 并且通知正确显示在状态栏上。

我的问题在于(2):

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

在解析之前我是否可以采取任何措施来拦截通知,或者我还能做些什么来解决我的问题?

ps:我需要通过“alert”

从服务器发送消息

韩国社交协会,

4 个答案:

答案 0 :(得分:28)

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

而是创建自己的BroadCastReceiver并将标题发送为例如在杰森的“头”。然后,您可以在onReceive处理程序中控制何时以及要显示的内容。

e.g。

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);
    }
}

答案 1 :(得分: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 );
    }
}

答案 2 :(得分:1)

不要忘记在AndroidManifest.xml中注册自定义BroadcastReceiver,如Parse.com的文档所示:

  

如果您的代码位于com.example包中,并且您想为com.example.UPDATE_STATUS操作注册接收器,则可以在ParseBroadcastReceiver块结束后立即将以下XML添加到AndroidManifest.xml文件中你之前创建的:

<receiver android:name="com.example.MyCustomReceiver" android:exported="false">
  <intent-filter>
    <action android:name="com.example.UPDATE_STATUS" />
  </intent-filter>
</receiver>
  

只要收到带有com.example.UPDATE_STATUS操作参数的推送通知,就会调用您的自定义接收器。出于安全考虑,Parse SDK确保此意图只能由应用程序中的接收者处理。您还应确保在元素上设置android:exported属性,以防止其他应用程序将推送发送到您的接收器。

答案 3 :(得分:0)

Hannes是正确的,但无论应用程序是否正在运行,该解决方案仍会生成通知。

要回答您的完整问题,请使用Hannes&#39;代码与此结合使用:How can I tell if Android app is running in the foreground?

只有在应用程序未运行时才会调用generateNotification()。