【问题标题】:Parse notification displayed, open action doesn't start activity显示解析通知,打开操作未启动活动
【发布时间】:2014-11-15 10:23:39
【问题描述】:

我有一些 Parse 代码可以接收推送数据并设置一个可以正常工作的通知,但问题是 onPushOpen 声明。我想在我的应用中打开一个特定的活动,但 onPushOpen 似乎永远不会被调用或只是不起作用?

这是我的代码:

public class NotificationReceiver extends ParsePushBroadcastReceiver {
    private static final String TAG = "MYTAG";


   @Override
    public void onReceive(Context context, Intent intent) {
       Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
        Log.i(TAG, "Notification received");
        Bundle extras = intent.getExtras();
        String message = extras != null ? extras.getString("com.parse.Data") : "";
        JSONObject jObject;
        String alert = null;
        String title = null;
        try {
            jObject = new JSONObject(message);
            alert = (jObject.getString("alert"));
            title = (jObject.getString("title"));
        }
        catch (JSONException e) {
            e.printStackTrace();
        }

        Log.i(TAG,"alert is " + alert);
        Log.i(TAG,"title is " + title);
        NotificationCompat.Builder notification =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(alert)
                        .setContentText(title);


        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, notification.build());
        Log.i(TAG, "Notification created");

   }


    @Override
    protected void onPushOpen(Context context, Intent intent) {
        Log.i(TAG, "Notification clicked");
        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

我似乎无法弄清楚为什么 onPushOpen 没有启动我请求的活动 (MainActivity)。任何帮助将不胜感激。

【问题讨论】:

  • 没有错误。推送/点击/单击通知时没有任何反应
  • 你知道onPushOpen()被调用了吗?
  • 我没有。据我在解析文档中发现,当您单击通知时会调用 onPushOpen。但是我没有看到我在方法开始时写的日志消息...

标签: android android-intent notifications parse-platform push-notification


【解决方案1】:

我在使用onPushOpen() 方法时遇到了同样的问题,但是如果您只是想打开您的活动,则需要一些解决方法。您可以通过使用Pending Intent 来解决问题,如下所示:

NotificationCompat.Builder notification =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(alert)
                    .setContentText(title);

// create intent to start your activity
Intent activityIntent = new Intent(context, MainActivity.class);

// create pending intent and add activity intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);

// Add as notification
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, notification.build());

因此没有必要调用onPushOpen() 方法,因为一旦点击通知,待处理的意图就会启动您的活动。

【讨论】:

    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多