如何显示通知栏?

时间:2019-12-13 06:11:17

标签: java android

我的程序未在android compilesdk28中显示通知栏。我的代码如下。如何解决该问题,以及如何在所有手机中显示通知?

final String url = userDetailsItem.getIp();

               System.out.println("url..."+url);

                if (Remaindays .equals("53"))
                    System.out.println("url..."+url);
                {
                    Context context = null;
                    String CHANNEL_ID = createNotificationChannel(context);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
                        .setSmallIcon(R.drawable.lesr)
                        .setContentTitle("my app")
                        .setContentText(""+url)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(Remaindays+"Days "))
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);        
                }

  public static String createNotificationChannel(Context context) {

        // NotificationChannels are required for Notifications on O (API 26) and above.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            // The id of the channel.
            String CHANNEL_ID = "Channel_id";

            // The user-visible name of the channel.
            CharSequence channelName = "Application_name";
            // The user-visible description of the channel.
            String channelDescription = "Application_name Alert";
            int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
            boolean channelEnableVibrate = true;
            //            int channelLockscreenVisibility = Notification.;

            // Initializes NotificationChannel.
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, channelName, channelImportance);
            notificationChannel.setDescription(channelDescription);
            notificationChannel.enableVibration(channelEnableVibrate);
            //            notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);

            // Adds NotificationChannel to system. Attempting to create an existing notification
            // channel with its original values performs no operation, so it's safe to perform the
            // below sequence.
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);

            return CHANNEL_ID;
        } else {
            // Returns null for pre-O (26) devices.
            return null;
        }
    }

2 个答案:

答案 0 :(得分:1)

API => 28需要通知通道

https://developer.android.com/reference/android/app/NotificationChannel

如果您想要视频链接

https://www.youtube.com/watch?v=tTbd1Mfi-Sk

模板:

Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent intent = new Intent(getApplicationContext(), DummychatActivty.class);

    intent.putExtra("user_id", Config.to_user_id);
    intent.putExtra("profile_img_Url", Config.profile_img_Url);
    intent.putExtra("user_name", Config.to_user_name);
    intent.putExtra("user_image_url", Config.to_user_image_url);


    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String CHAT_NOTIFICATION_CHANNEL_ID = "101";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        @SuppressLint("WrongConstant") NotificationChannel Chat_notificationChannel = new NotificationChannel(CHAT_NOTIFICATION_CHANNEL_ID, "Chat Notifications", NotificationManager.IMPORTANCE_MAX);

        //Configure Notification Channel
        //  notificationChannel.setDescription("General Notifications");
        Chat_notificationChannel.setLightColor(Color.RED);
        Chat_notificationChannel.enableLights(true);
        Chat_notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        Chat_notificationChannel.enableVibration(true);

        notificationManager.createNotificationChannel(Chat_notificationChannel);

    }

    NotificationCompat.Builder chat_notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHAT_NOTIFICATION_CHANNEL_ID)
            .setVibrate(new long[]{0, 100})
            .setPriority(Notification.PRIORITY_MAX)
            .setLights(Color.RED, 3000, 3000)
            .setAutoCancel(true)
            .setContentTitle(Config.fcm_headline)
            .setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis())
            .setColor(getResources().getColor(R.color.colorPrimary))
            .setSmallIcon(R.drawable.push)
            .setLargeIcon(bitmap)
            .setGroup("CHAT")
            .setContentText(Config.content)
            .setAutoCancel(true)
            .setSound(defaultSound);

    chat_push_count = chat_push_count + 1;
    notificationManager.notify(chat_push_count, chat_notificationBuilder.build());


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        chat_notificationBuilder.setChannelId("101");

    }

答案 1 :(得分:0)

  private void sendNotification(Notification nobj) {
    Intent intent = new Intent(context, NotificationActivity.class);
    intent.putExtra("Notification", "click");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 
  PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = 
    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(context, defaultSoundUri);
    r.play();
    NotificationCompat.Builder notificationBuilder = new 
   NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(Html.fromHtml(nobj.title))
            .setContentText(Html.fromHtml(nobj.desc))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setChannelId("my_channel_01")
            .setContentIntent(pendingIntent);
    generateNotification(notificationBuilder);

}

请检查这可以在我的实时项目中获取帮助及其工作代码

 public void generateNotification(NotificationCompat.Builder notificationBuilder) {
    NotificationManager notificationManager =
            (NotificationManager) 
   context.getSystemService(Context.NOTIFICATION_SERVICE);
    String CHANNEL_ID = "my_channel_01";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = context.getString(R.string.app_name);
   // The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, 
    importance);
    // Create a notification and set the notification channel.
        notificationManager.createNotificationChannel(mChannel);
    }
    PreferenceHelper preferenceHelper = PreferenceHelper.getInstance(context);
    preferenceHelper.setNotificationId(preferenceHelper.getNotificationId() + 1);
    notificationManager.notify(preferenceHelper.getNotificationId(), 
   notificationBuilder.build());
  }