单击时通知未重定向

时间:2019-11-13 06:21:05

标签: android push-notification android-pendingintent

重定向工作正常,但是如果立即单击“推送通知”(例如2秒钟左右),则不会发生重定向。我第二次确保在任何地方等待10-15秒左右,那时候效果很好。 谁能帮我解决这个问题。

Notification notification;
        Intent notificationIntent = BMSNotificationIntent.getNotificationIntent(notificationData, mContext,
                mSharedPreferencesManager, true);

        notificationIntent.putExtras(rawNotificaionData);
        PendingIntent NotificationPendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
        notificationBuilder.setPriority(Notification.PRIORITY_MAX);
        notificationBuilder
                .setSmallIcon(R.drawable.status_icon)
                .setColor(ContextCompat.getColor(mContext, R.color.transparent))
                .setContentIntent(NotificationPendingIntent)
                .setTicker(notificationData.getCleverTapTitle())
                .setAutoCancel(isAutoCancelable())
                .setContentTitle(notificationData.getCleverTapTitle())
                .setContentText(notificationData.getCleverTapMessage())
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_SOUND);


        notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
        notificationBuilder.setChannelId(NotificationChannelName.GENERAL);

        /*Create a Delete Intent that will be called when user removes the notification by swiping or by clear*/
        notificationBuilder.setDeleteIntent(getNotificationDeletePendingIntent());


        notification = notificationBuilder.build();
        notification.flags |= NotificationCompat.FLAG_AUTO_CANCEL;
        if (BMSUiUtility.isNotificationFromInbox(rawNotificaionData, mSharedPreferencesManager)) {
            mNotificationManager.notify(notificationId, notification);
        } else {
            mNotificationManager.notify(0, notification);
        }

2 个答案:

答案 0 :(得分:0)

请使用以下代码段:)

Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = 
stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

其他

Intent intent = new Intent(this, SomeActivity.class);

// Creating a pending intent and wrapping our intent
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,       
intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
// Perform the operation associated with our pendingIntent
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}

答案 1 :(得分:0)

解决方案:

                Intent intent1 = new Intent(activity, ResultActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                intent1.putExtra(Extras.EXTRA_NOTIFICATION_ID, true);
                intent1.putExtra("notificationId", EXPORT_NOTIFICATION_ID);

                Log.i(TAG, "onPostExecute: app is BackGround ");
                pendingIntent = PendingIntent.getActivity(activity, EXPORT_NOTIFICATION_ID, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(pendingIntent);
                mNotifyManager.cancel(EXPORT_NOTIFICATION_ID);


                Intent buttonIntent = new Intent(activity, NotificationReceiver.class);
                buttonIntent.putExtra("notificationId", EXPORT_NOTIFICATION_ID);
                PendingIntent dismissIntent = PendingIntent.getBroadcast(activity, 0, buttonIntent, 0);

                builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent);
                builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent);



    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
    notificationBuilder.setPriority(Notification.PRIORITY_MAX);
    notificationBuilder
            .setSmallIcon(R.drawable.status_icon)
            .setColor(ContextCompat.getColor(mContext, R.color.transparent))
            .setContentIntent(NotificationPendingIntent)
            .setTicker(notificationData.getCleverTapTitle())
            .setAutoCancel(isAutoCancelable())
            .setContentTitle(notificationData.getCleverTapTitle())
            .setContentText(notificationData.getCleverTapMessage())
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_SOUND);


    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
    notificationBuilder.setChannelId(NotificationChannelName.GENERAL);

    /*Create a Delete Intent that will be called when user removes the notification by swiping or by clear*/
    notificationBuilder.setDeleteIntent(getNotificationDeletePendingIntent());


    notification = notificationBuilder.build();
    notification.flags |= NotificationCompat.FLAG_AUTO_CANCEL;
    if (BMSUiUtility.isNotificationFromInbox(rawNotificaionData, mSharedPreferencesManager)) {
        mNotificationManager.notify(notificationId, notification);
    } else {
        mNotificationManager.notify(0, notification);
    }

使用以下代码取消您的 MainActivity onDestroy Mathod调用的通知:

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

在此代码中,始终有用于通知的相同ID。如果您有其他需要取消的通知,则必须保存用于创建通知的ID。