【问题标题】:Notification is not redirecting when clicked单击时通知未重定向
【发布时间】:2019-11-13 06:21:05
【问题描述】:

重定向工作正常,但如果在 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);
        }

【问题讨论】:

  • 请发布您的代码
  • @lzzuddiin 已添加
  • @dev_mg99 请检查我的回答可能对你有帮助

标签: android push-notification android-pendingintent


【解决方案1】:

请使用下面的代码片段:)

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

【讨论】:

  • 如果我正在使用这个 // 执行与我们的pendingIntent相关的操作 pendingIntent.send();这将自动重定向到活动关联意图而不执行通知点击。
【解决方案2】:

解决方案:

                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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多