在Android上未阅读消息时出现错误通知

用户名

我正在将Firebase cloudmessaging用于我的Android应用程序通知,所以我的问题是,如果用户取消了该通知,则我发送通知时,如果我单击后发送的下一个通知正在打开已被取消的第一个通知,即使我发送了第三个通知,并且用户同时关闭了第一个和第二个通知,如果单击第三个通知,它将打开第一个通知。我正在将Firebase云消息传递与数据一起使用,并发送(标题,摘录,图像,链接)。在通知栏中,所有内容都很酷并且正确无误,但是当单击链接时,链接更改并且Webview将打开第一个通知。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"),
                    Integer.parseInt(remoteMessage.getData().get("topic")), remoteMessage.getData().get("link"), remoteMessage.getData().get("imageUrl"), Integer.parseInt(remoteMessage.getData().get("id")));

            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                scheduleJob();
            } else {
                // Handle message within 10 seconds
                handleNow();
            }

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(),
                    0, " ", " ", 0);
        }
    }
    // [END receive_message]

    /**
     * Schedule a job using FirebaseJobDispatcher.
     */
    private void scheduleJob() {
        // [START dispatch_job]
        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
        Job myJob = dispatcher.newJobBuilder()
                .setService(MyJobService.class)
                .setTag("my-job-tag")
                .build();
        dispatcher.schedule(myJob);
        // [END dispatch_job]
    }

    /**
     * Handle time allotted to BroadcastReceivers.
     */
    private void handleNow() {
        Log.d(TAG, "Short lived task is done.");
    }

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    @TargetApi(Build.VERSION_CODES.O)
    private void sendNotification(String messageTitle, String messageBody, int topic, String link, String imageUrl, int id) {

        PendingIntent pendingIntent;
        if (topic == 1){
            Intent intent = new Intent(this, WebActivity.class);
            // Create the TaskStackBuilder and add the intent, which inflates the back stack
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addNextIntentWithParentStack(intent);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("link", link);
            intent.putExtra("title", messageTitle);
            // Get the PendingIntent containing the entire back stack
            pendingIntent =
                    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
        }else{
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("link", link);
            intent.putExtra("topic", topic);
            pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);
        }

        String channelId = getString(R.string.default_notification_channel_id);

        InputStream in;
        Bitmap myBitmap = null;
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            in = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(in);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setPriority(NotificationManager.IMPORTANCE_DEFAULT)
                        .setChannelId(channelId)
                        .setSmallIcon(R.drawable.ic_stat_name)
                        .setLargeIcon(myBitmap)
                        .setContentTitle(messageTitle)
                        .setContentText(messageBody)
                        .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent))
                        .setAutoCancel(true)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(messageTitle))
                        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(myBitmap))
                        .setGroupSummary(true)
                        .setGroup(String.valueOf(topic))
                        .setContentIntent(pendingIntent);

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

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = "The Channel";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(channelId, name, importance);
            channel.setDescription(description);
            channel.setShowBadge(true);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
            notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(String.valueOf(topic), "Articles"));
        }

        notificationManager.notify(id /* ID of notification */, notificationBuilder.build());
    }
}

预期的结果是,如果用户取消了第一个通知,则单击该按钮,Webview将打开从该通知发送的第二个信息。

用户名

因此,经过大量研究,我发现每次创建新意图时,我都必须更新其意图PendingIntent.FLAG_UPDATE_CURRENT并更改其request code意图,这就是将来任何出现此问题的新代码:

PendingIntent pendingIntent;
        if (topic == 1){
            Intent intent = new Intent(this, WebActivity.class);
            // Create the TaskStackBuilder and add the intent, which inflates the back stack
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addNextIntentWithParentStack(intent);
            intent.putExtra("link", link);
            intent.putExtra("title", messageTitle);
            intent.setAction("actionstring" + System.currentTimeMillis());
            // Get the PendingIntent containing the entire back stack
            pendingIntent =
                    stackBuilder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);
        }else{
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("link", link);
            intent.putExtra("topic", topic);
            pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在批处理中从Java在PostgreSQL上执行存储过程时出现错误消息,提示“未预期结果”?

在 Windows 上执行 pip install Psychopy 时出现错误消息

Django-提交时错误消息未显示在表单上

阅读时出现Python权限错误

点击Android时出现多个通知错误内容

显示html消息时出现Android Webview错误

Android 3.5.3上出现“未配置Kotlin”错误

Android推送通知消息未显示

使用RabbitMQ延迟消息交换插件时出现“函数未导出”错误

访问Windows OpenSSH SFTP服务器上的文件时出现错误“错误消息”

在Android的Java Websocket服务器上收到消息时未创建Toast消息

使用android-upload-service时出现“ startForeground错误通知”错误

通知出现在“错误”屏幕上

尝试从Android共享Facebook上的链接时出现错误

单击Android上的单选按钮时出现错误

仅在Android上查询时出现SQL逻辑错误

发送推送通知时出现“错误”:“ InvalidRegistration”

实施 Firebase (Flutter) 通知时,出现错误

空行上出现预期的声明错误消息

在root登录时出现持续错误消息

创建新的登录消息时出现错误

R 启动时出现错误消息

使用tensorflow时出现错误消息

检索节点时出现错误消息

在设备上未显示注册错误消息

如何在Android上阅读传入的MMS和SMS消息

什么是--use-feature = 2020-resolver?在Ubuntu上安装Jupyter时出现错误消息

尝试从Windows 7 PC上的USB闪存盘引导Ubuntu 16.04时出现错误消息

在MSI-Z370-A PRO上安装OS时出现BIOS ACPI错误消息