它只在低于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;
}
}
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());
}
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);
您必须在构建Notification对象后调用notify
,如下所示。
mNM.notify(notificationId/*UNIQUE ID*/, mNotify);
下面是使用代码的片段。
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);
}
}
}
}