仅在启动活动后才弹出Android应用通知

问题描述

我编写了一个工作程序类,以在后台活动完成后显示通知,基本上是一个常规的服务器调用,以获取数据并更新到本地会议室数据库中。我将定期的工作人员类别间隔设置为每15分钟执行一次,它的执行与预期的相同,但是根本不显示任何通知。在用户再次启动该应用程序的那一刻,所有通知都开始一个一个地下雨。这就是我在做什么。

创建定期工作

PeriodicWorkRequest pushPullWork = new PeriodicWorkRequest.Builder(PushPullWorker.class,15,TimeUnit.MINUTES)
.addTag("sync_withserver")
.setInputData(inputData) // Passing data to PushPullWorker class
.setinitialDelay(10,TimeUnit.SECONDS) // First request to server after 10 seconds of launching the app.
.build();

WorkManager.getInstance(context).enqueueUniquePeriodicWork("sync_withserver",ExistingPeriodicWorkPolicy.KEEP,pushPullWork);

创建通知频道

private void createNotificationChannel(String channelname) {

        // Creation of notification channel required if the build version is greater than or equal to Orieo
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence name = channelname;
            String description = "Case and Message Channel";
            int importance = notificationmanager.IMPORTANCE_HIGH;

            NotificationChannel channel = new NotificationChannel(channelname,name,importance);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC );
            channel.setDescription(description);

            notificationmanager notificationmanager = context.getSystemService(notificationmanager.class);
            notificationmanager.createNotificationChannel(channel);

        }
    }

通知用户

private void showNotification(String message,String channelname,String messagetype){

        Intent intent = new Intent(context,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context,intent,0);

        Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(),R.mipmap.icon );

        Notification builder = new NotificationCompat.Builder(context,channelname)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(messagetype)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setDefaults(Notification.DEFAULT_ALL)
                .setChannelId("MN23021985")
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLargeIcon(largeIcon)
                .setAutoCancel(true).build();

        notificationmanagerCompat notificationmanager = notificationmanagerCompat.from(context);

        notificationmanager.notify(( int ) System. currentTimeMillis (),builder);

    }

呼叫-成功的服务器呼叫之后

createNotificationChannel("mynuniqueapp_notifications");
showNotification(caseApiResponse.description,"mynuniqueapp_notifications","New Case(s)");

我们非常感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)