首页 > 解决方案 > 警报未显示通知

问题描述

它仅在低于 26 的 API 上显示通知。它不显示高于 API 26 的通知。

我是新来的,我是否需要添加频道才能获得通知,如果需要如何添加频道。

我是编程新手,如果用完整的现有代码发布答案会有所帮助。这是我的 RingtonePlayingService.java,提前谢谢。

public class RingtonePlayingService extends Service {

private boolean isRunning;
private Context context;
MediaPlayer mMediaPlayer;


@Nullable
@Override
public IBinder onBind(Intent intent) {
    Log.e("MyActivity", "In service");
    return null;
}


@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{

    final NotificationManager mNM = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);

    Intent intent1 = new Intent(this.getApplicationContext(), 
MainActivity2.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 
0);

    Notification mNotify  = new Notification.Builder(this)
            .setContentTitle("WAKE UP" + "!")
            .setContentText("No Alarm Needed My Passion Wakes Me")
            .setSmallIcon(R.drawable.icon)
            .setContentIntent(pIntent)
            .setAutoCancel(true)
            .build();

String state =  
Objects.requireNonNull(intent.getExtras()).getString("extra");

    Log.e("what is going on here  ", state);

    assert state != null;
    switch (state) {
        case "no":
            startId = 0;
            break;
        case "yes":
            startId = 1;
            break;
        default:
            startId = 0;
            break;
    }


    int startId1;
    if(!this.isRunning && startId == 1) {
        Log.e("if there was not sound ", " and you want start");

        int min = 1;
        int max = 5;

        Random r = new Random();
        int random_number = r.nextInt(max - min + 1) + min;
        Log.e("random number is ", String.valueOf(random_number));

        if (random_number == 1) {
            mMediaPlayer = MediaPlayer.create(this, 
  R.raw.rins_3);
        }
        else if (random_number == 2) {
            mMediaPlayer = MediaPlayer.create(this, 
  R.raw.rics_2);

        }
            else if (random_number == 3) {
            mMediaPlayer = MediaPlayer.create(this, 
   R.raw.rics_1);
        }
  else if (random_number == 4) {
                mMediaPlayer = MediaPlayer.create(this,   
    R.raw.rs_4);

            }
        else if (random_number == 5) {
            mMediaPlayer = MediaPlayer.create(this, 
  R.raw.rics_5);


        }
        else {
            mMediaPlayer = MediaPlayer.create(this, 
 R.raw.rs_1);
        }
        //mMediaPlayer = MediaPlayer.create(this, 
 R.raw.r_1);

        mMediaPlayer.start();


        Objects.requireNonNull(mNM).notify(0, mNotify);

        this.isRunning = true;
        startId1 = 0;

    }
    else if (!this.isRunning){
        Log.e("if there was not sound ", " and you want end");

        this.isRunning = false;
        startId1 = 0;

    }

    else if (startId == 1){
        Log.e("if there is sound ", " and you want start");

        this.isRunning = true;
        startId1 = 0;

    }
    else {
        Log.e("if there is sound ", " and you want end");

        mMediaPlayer.stop();
        mMediaPlayer.reset();

        this.isRunning = false;
        startId1 = 0;
    }


    Log.e("MyActivity", "In the service");

    return START_NOT_STICKY;

  }


  @Override
   public void onDestroy() {
    Log.e("JSLog", "on destroy called");
    super.onDestroy();

    this.isRunning = false;
  }

 }

标签: androidfirebasefirebase-cloud-messaging

解决方案


Android O 你需要创建通道 ID 并在创建通知构建器时传递通道 ID。

您需要创建类似这样的通知通道。

public  void createNotificationChannel(NotificationManager manager, String channelID, String description) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (manager.getNotificationChannel(channelID) == null) {
            NotificationChannel channel = new NotificationChannel(channelID, description,
                    NotificationManager.IMPORTANCE_LOW);
            channel.setDescription(description);
            manager.createNotificationChannel(channel);
        }
    }
}

在创建通知构建器时,您需要传递通道 ID

      Notification mNotify  = new Notification.Builder(this,<ChannelID>)
                .setContentTitle("WAKE UP" + "!")
                .setContentText("No Alarm Needed My Passion Wakes Me")
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(pIntent)
                .setAutoCancel(true)
                .build();

                mNM.notify(notificationId, mNotify);

推荐阅读