本文介绍了无法在 Android 10 上运行通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的通知代码适用于 Android API 25,但当我在 Android 10 上尝试时,它不起作用.我没有收到任何错误,它只是什么也没做.在过去的两个月里,我一直试图弄清楚,但没有成功.我没有在网上找到任何有用的东西,并尝试了我能想到的一切,但没有任何效果.请提前提供帮助和感谢.

My code for a notification works on Android API 25, but when I tried it on Android 10 it didn't work. I received no errors, it just simply did nothing at all. I have been trying to figure it out for the last two months without success. I failed to find anything helpful online and have tried everything I could possibly think of, but nothing has worked. Please help and thanks in advance.

    Intent notificationIntent = new Intent(context, bibleReader.class);
    Bundle bundle = new Bundle();
    notificationIntent.putExtras(bundle);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


    Notification.BigTextStyle bigText = new Notification.BigTextStyle();
    bigText.bigText(by);
    bigText.setSummaryText(bv);

    Notification.Builder NotificationBuilder;

    // check Android API verson and do as needed

    if (android.os.Build.VERSION.SDK_INT  >= android.os.Build.VERSION_CODES.O) {
    NotificationBuilder = new Notification.Builder(context, "ID_BN");
    } else {
    NotificationBuilder = new Notification.Builder(context);
    }
    Notification.Builder mBuilder = NotificationBuilder;

    mBuilder.setSmallIcon(R.drawable.nicon);
    mBuilder.setContentTitle("A Word");
    mBuilder.setContentText(bv);
    mBuilder.setStyle(bigText);
    mBuilder.setAutoCancel(true);
    mBuilder.setTicker("text");
    mBuilder.setContentIntent(contentIntent);  (android.os.Build.VERSION.SDK_INT  >= android.os.Build.VERSION_CODES.O) {
    mBuilder.setChannelId(CHANNEL_ID);
    }

    mNotificationManager.notify(1, mBuilder.build());

推荐答案

此代码在任何版本的 android 中都能正常工作.

this code work fine in any version of android.

您可以将此代码放入任何 brodcastresciver 或类似的东西(服务等...)

Intent pIntent = new Intent(context, SecondActivity.class);
    mpIntent = PendingIntent.getActivity(context, 0, pIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    int notificationId = intent.getIntExtra("notificationId", 0);
    String message = intent.getStringExtra("todo");

    Intent mainIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent,PendingIntent.FLAG_ONE_SHOT );

    String channelId="channel_id";
    CharSequence name="channel_name";
    String descruption="description";
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel channel=new NotificationChannel(channelId,name,NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(descruption);
        NotificationManager notificationManager=context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }


    NotificationCompat.BigTextStyle bStyle = new NotificationCompat.BigTextStyle()
            .setBigContentTitle("Your Title");

    Notification notification=new NotificationCompat.Builder(context,channelId)
                .setSmallIcon(R.drawable.notif_icon)
                .setVibrate(new long[]{100, 500, 500, 500, 500, 500})
              .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
                 .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                 .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentTitle("Your Title")
                .setContentText(message)
                .setDeleteIntent(contentIntent)
                .setStyle(bStyle)
                .setContentIntent(mpIntent)
                .setAutoCancel(true)
                .build();
    NotificationManagerCompat notificationManagerCompat=NotificationManagerCompat.from(context);
    notificationManagerCompat.notify(notificationId,notification);

`玩得开心:)

这篇关于无法在 Android 10 上运行通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:51
查看更多