【问题标题】:BigPictureStyle Notification not working in AndroidXBigPictureStyle 通知在 AndroidX 中不起作用
【发布时间】:2020-05-17 05:50:53
【问题描述】:

代码: 这是单击按钮时弹出通知的代码

 notificationbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(getContext(), "noti", Toast.LENGTH_SHORT).show();
                    Intent i=new Intent(getActivity(),OrderTrack.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(),(int) Calendar.getInstance().getTimeInMillis(),i,0);

                    Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.half_bicycle);
                    Notification builder=new NotificationCompat.Builder(getActivity(),"1")
                            .setSmallIcon(R.drawable.half_bicycle)
                            .setContentTitle("title")
                            .setContentText("Text")
                            .setLargeIcon(bitmap)
                            .setStyle(new NotificationCompat.BigPictureStyle()
                                    .bigPicture(bitmap)
                                    .bigLargeIcon(null))
                            .setPriority(NotificationCompat.PRIORITY_HIGH)
                            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                            .setContentIntent(pendingIntent)
                            .setAutoCancel(true)
                            .setOnlyAlertOnce(true)
                            .build();


                    Notification notif = new Notification.Builder(getContext())
                            .setContentTitle("New photo from ")
                            .setContentText("subject")
                            .setSmallIcon(R.drawable.half_bicycle)
                            .setLargeIcon(bitmap)
                            .setStyle(new Notification.BigPictureStyle()
                                    .bigPicture(bitmap))
                            .build();
                }
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
                notificationManager.notify(101, notif);
            });

Here I am trying to use BigPictureSt

yle 通知。我已经尝试了两种代码变体。但两者都没有给出任何结果/通知。

以上所有代码都在一个片段中。这是它不起作用的原因吗?

我已将 App 样式设置为 NoActionBar。这是它不起作用的原因吗?

请用正确的方法回答这个问题!!!!

添加以下代码并不能解决问题

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
                    notificationManager.notify(101, notif);

【问题讨论】:

    标签: android push-notification notifications android-notifications


    【解决方案1】:

    代码很好,没有任何问题,但最重要的是您忘记显示通知本身,即您没有使用创建的对象。

    所以显示这样的通知...

     NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
     notificationManager.notify(101, notif);//101 is notification id 
    

    编辑:

    如果您的目标是 Android Oreo 及更高版本,您需要创建这样的通知渠道。使用此函数创建通知通道,并在创建通知时在通知上设置此通道 id...

    String channelId = "some_channel_id";
    
    
    
       private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
    

    完整代码:

    notificationbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
               createNotificationChannel(); //Create notification channel
    
                    Toast.makeText(getContext(), "noti", Toast.LENGTH_SHORT).show();
                    Intent i=new Intent(getActivity(),OrderTrack.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(),(int) Calendar.getInstance().getTimeInMillis(),i,0);
    
                    Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.half_bicycle);
                    Notification builder=new NotificationCompat.Builder(getActivity(),"1")
                            .setSmallIcon(R.drawable.half_bicycle)
                            .setContentTitle("title")
                            .setContentText("Text")
                            .setLargeIcon(bitmap)
                            .setChannelId(CHANNEL_ID) // Channel Id 
                            .setStyle(new NotificationCompat.BigPictureStyle()
                                    .bigPicture(bitmap)
                                    .bigLargeIcon(null))
                            .setPriority(NotificationCompat.PRIORITY_HIGH)
                            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                            .setContentIntent(pendingIntent)
                            .setAutoCancel(true)
                            .setOnlyAlertOnce(true)
                            .build();
    
    
                    Notification notif = new Notification.Builder(getContext())
                            .setContentTitle("New photo from ")
                            .setContentText("subject")
                            .setSmallIcon(R.drawable.half_bicycle)
                            .setLargeIcon(bitmap)
                            .setChannelId(CHANNEL_ID) // Channel Id 
                            .setStyle(new Notification.BigPictureStyle()
                                    .bigPicture(bitmap))
                            .build();
    
         NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
         notificationManager.notify(101, notif);//101 is notification id
                }
            });
    

    欲了解更多信息,请访问:https://developer.android.com/training/notify-user/channels

    【讨论】:

    • 添加以下代码并不能解决问题
    • NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity()); notificationManager.notify(101, notif);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    相关资源
    最近更新 更多