我正在使用以下内容向 Android 上的用户呈现通知,目前工作正常,但我遇到一个问题,即通知显示在状态栏中,但不像设备上的 Facebook 或 WhatsApp 通知那样以提示形式出现?我收到通知,但必须下拉状态栏才能查看。我想知道是否有办法让它以气泡格式显示在屏幕顶部,或者这是否因手机设置而异?
代码附在下面:
主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addNotification(10,"eventname","roomname");
addNotification(25,"eventname2","roomname2");
}
public void addNotification(int test, String test2, String test3){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("test",test2);
intent.putExtra("test2",test3);
final int _id = 50;
Random random = new Random();
final int randomInt = random.nextInt();
System.out.println("random integer:" + randomInt);
PendingIntent appIntent = PendingIntent.getBroadcast(this, randomInt, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, test);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), appIntent);
}
}
警报接收器
public class AlarmReceiver extends BroadcastReceiver{
private static final String CHANNEL_ID = "com.singhajit.notificationDemo.channelId";
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, NotificationActivity.class);
String passed = intent.getStringExtra("test");
String passed2 = intent.getStringExtra("test2");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(NotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
String messageBody = "Your event " + passed + " is about to start in 15 minutes, in room "+passed2;
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(context);
builder.setStyle(new Notification.BigTextStyle(builder)
.bigText(messageBody)
.setBigContentTitle("UA Reloaded Event Starting")
.setSummaryText("Tap To View Info"))
.setContentText(messageBody)
.setSmallIcon(R.drawable.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_MAX);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_ID);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = "NotificationDemo";
String description = "NotificationDemo";
int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
NotificationChannel channel = new NotificationChannel("1", name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
// NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, builder.build());
}
}
您更新了频道重要性,这是不可能的,如文档中所述 (https://developer.android.com/training/notify-user/channels#CreateChannel)。
因此,您的问题应该通过将
channelId
更改为 "1"
以外的其他内容来解决,因为通道的 id
必须是唯一的。
如果您仍然需要答案或需要其他人才能将通知显示为提醒,您必须将您的频道 ID 添加到构建器。
.setChannelId(CHANNEL_ID)
像这样:
val notification = NotificationCompat.Builder(getContext(), CHANNEL_ID)
.setSmallIcon(...)
.setContentTitle(getContext().getString(R.string.app_name))
...
.setChannelId(CHANNEL_ID)
...
.build()
不要忘记NotificationChannel的重要性和通知优先级(如果需要,将它们设置为高/最大)
解决方案很简单,当我们使用NotificationChannel并将重要性设置为高时,我们必须卸载该应用程序然后再次安装它,否则它将无法工作。
这是我的 kotlin 类,你需要的只是调用 notificate(title: String, text: String) 方法,如果你想在 java 中使用它,你可以转换它
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationManager
import android.content.Context
import android.support.v4.app.NotificationCompat
import beacon.geisoft.org.beacontrakerkotlin_rebuild.R
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.content.ContextCompat.getSystemService
import android.app.NotificationChannel
import android.app.PendingIntent
import android.content.Intent
import android.graphics.Color
import android.media.RingtoneManager
import android.support.v4.content.ContextCompat.getSystemService
import android.support.v4.app.NotificationManagerCompat
import beacon.geisoft.org.beacontrakerkotlin_rebuild.activities.MainActivity
import android.preference.PreferenceManager
import android.content.SharedPreferences
class Notifications (var context: Context){
/**
* Send notification to the client device
* @param text String
*/
@SuppressLint("PrivateResource")
private fun notificate(title: String, text: String, id: Int, notificationManager: NotificationManager) {
val intent1 = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 123, intent1, PendingIntent.FLAG_UPDATE_CURRENT)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel("beacon.geisoft.org.beacontraker_rebuild") == null) {
val chan2 = NotificationChannel("beacon.geisoft.org.beacontraker_rebuild", "Pazienti", NotificationManager.IMPORTANCE_HIGH)
chan2.lightColor = Color.BLUE
chan2.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager.createNotificationChannel(chan2)
/*
notificationManager.createNotificationChannel(NotificationChannel("beacon.geisoft.org.beacontraker_rebuild",
"Pazienti", NotificationManager.IMPORTANCE_HIGH))*/
}
val builder = NotificationCompat.Builder(context, "beacon.geisoft.org.beacontraker_rebuild")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setContentTitle(title) // required
.setContentText(text) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.beaconicon32) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.beaconicon64))
.setSound(defaultSoundUri)
}else {
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.beaconicon32) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.beaconicon64))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(defaultSoundUri)
}
notificationManager.notify(id, builder.build());
}
fun notificate(title: String, text: String, id: Int){
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
notificate(title, text, id, notificationManager!!)
}
fun notificate(title: String, text: String){
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
var num: Int
do {
num = (Math.random() * 100).toInt()
} while (notificationExist(notificationManager!!, num))
notificate(title, text, num, notificationManager)
}
fun notificationExist(notificationManager: NotificationManager, id: Int): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val notifications =
notificationManager.activeNotifications
for (notification in notifications) {
if (notification.getId() == id) {
return true
}
}
}
return false
}
}