警报未显示通知

时间:2018-11-15 12:48:01

标签: android firebase firebase-cloud-messaging

仅在26以下的API上显示通知。在26以上的API不显示通知。

我是新来的,是否需要添加频道以获取通知,如果是的话,如何添加频道。

我是编程新手,如果发布包含完整现有代码的答案,将会有所帮助。 这是我的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;
  }

 }

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您必须按如下所示构建Notification对象后调用notify

mNM.notify(notificationId/*UNIQUE ID*/, mNotify);

答案 2 :(得分:0)

这是使用您的代码的代码段。

 public class RingtonePlayingService extends Service {


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

    String channelID =  "NotificationChannelID";
    String description =  "Notification Channel Description goes here";
    createNotificationChannel(mNM,channelID,description);

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

  }



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

答案 3 :(得分:0)

private void showNotification(String msg) {

    int NOTIFICATION_ID = 0;
    PendingIntent contentIntent = null;
    NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, HomeActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentTitle("Title")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mBuilder.setAutoCancel(true);
    //sound
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mBuilder.setSound(uri);

    //vibrate
    long[] v = {500, 1000};
    mBuilder.setVibrate(v);

    Random random = new Random();
    NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
相关问题