[解析​​] [机器人]如何晚饭时,从应用程序正在运行显示preSS推送通知?机器人、应用程序、晚饭、正在运行

2023-09-04 09:47:34 作者:朕很萌′

我需要一个推送通知,以处理有两种方式:

I need to handle with a push notification in two ways:

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

1) Receive and put a notification on status bar of my android client when app is in background;

2)接收并通知处理没有表现出它在状态栏上时,我的应用程序是在前台;

2) Receive and handle with a notification without showing it on status bar when my app is in foreground;

有关(1)很简单,我把 和呼叫 PushService.setDefaultPushCallback(背景下,classObject); 和通知显示在状态栏正确

For (1) was very simple, I put the and call PushService.setDefaultPushCallback(context, classObject); and the notification appears on status bar correctly.

我的问题是与(2):

我试图创建一个自定义的BroadcastReceiver,但解析取通知我面前并将其显示在状态栏; 我试图关闭 PushService.setDefaultPushCallback(背景下,classObject) 在OnStart方法设置为空值classObject,但是当我这样做,我的接收机是从来没有打电话通知didn't出现; I tried to create a custom BroadCastReceiver, but parse takes the notification before me and shows it on status bar; I tried to turn off PushService.setDefaultPushCallback(context, classObject) on onStart method by setting null value for classObject, but when I did that, my receiver was never called and the notification didn´t appear;

有什么我可以做解析之前拦截通知或有另一件事我可以做些什么来解决我的问题?

Is there anything I could do to intercept notifications before parse or is there another thing I could do to solve my problem?

PS:我需要从服务器警报

ps: I need to send the message from the server with "alert"

韩国社交协会,

推荐答案

如果您使用警告或标题,在你的JSON数据,在com.parse.PushService会拦截并显示一个标准的通知。

If you use "alert" or "title" in your json data, the com.parse.PushService will intercept and display a standard notification.

而创建自己的BroadcastReceiver发送标题为如JSON中的头。然后,您可以在您的onReceive处理器控制何时以及要显示的内容。

Rather create your own BroadCastReceiver and send the title as e.g. "header" in json. You can then control in your onReceive handler when and what to display.

例如。

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