在android 10上获取通知时遇到问题

9nvpjoqh  于 2021-06-27  发布在  Java
关注(0)|答案(3)|浏览(522)

我的通知代码可以在androidapi25上运行,但是当我在android10上尝试它时,它不起作用。我没有收到任何错误,只是什么也没做。在过去的两个月里,我一直在想办法,但没有成功。我在网上找不到任何有用的东西,也尝试了所有我能想到的方法,但都没用。请提前帮助和感谢。

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());
0lvr5msh

0lvr5msh1#

在这个论坛上的其他人的帮助下,我能够整理出下面的代码。非常感谢大家!

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

    if(android.os.Build.VERSION.SDK_INT  >= android.os.Build.VERSION_CODES.O) {
        notificationChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
     }

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT  >= android.os.Build.VERSION_CODES.O) {
        mNotificationManager.createNotificationChannel(notificationChannel);
    }

    Notification.BigTextStyle bigText = new Notification.BigTextStyle();
    bigText.bigText(textHere);
    bigText.setSummaryText(textHere);

    Notification.Builder NotificationBuilder;

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

    mBuilder.setSmallIcon(R.drawable.nicon);
    mBuilder.setContentTitle("text");
    mBuilder.setContentText(textHere);
    mBuilder.setStyle(bigText);
    mBuilder.setAutoCancel(false);
    mBuilder.setContentIntent(contentIntent);

    if (android.os.Build.VERSION.SDK_INT  >= android.os.Build.VERSION_CODES.O) {
    mBuilder.setChannelId(CHANNEL_ID);
    }
    mNotificationManager.notify(1, mBuilder.build());
goqiplq2

goqiplq22#

对于oreo和以上的设备,请在创建通知通道后尝试如下所示。请看一下,有什么遗漏请告诉我。

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT  >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "A Word", NotificationManager.IMPORTANCE_LOW);
            mNotificationManager.createNotificationChannel(channel);
}
Notification notification = new NotificationCompat.Builder(getBaseContext(),CHANNEL_ID)
                            .setSmallIcon(R.drawable.nicon)
                            .setContentTitle("A Word")
                            .setAutoCancel(true)
                            .setContentText(bv)
                            .setContentIntent(contentIntent)
                            .build();
                    mNotificationManager.notify(0, notification);
cmssoen2

cmssoen23#

这段代码在任何版本的android中都可以正常工作。
你可以把这个代码放到任何brodcastersiver或者类似的东西(服务,等等…)

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

`玩得开心:)

相关问题