伙计们,我为Android应用创建了通知,我的代码在虚拟设备上工作,但在实际设备上不工作,请帮助我。
public void onClick(View v){
try {
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(R.mipmap.smile);
builder.setShowWhen(true);
builder.setContentText("This is my first notification");
builder.setContentTitle("My Notification");
builder.setAutoCancel(true);
Intent i = new Intent(MainActivity.this, SecondClass.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
stackBuilder.addParentStack(SecondClass.class);
stackBuilder.addNextIntent(i);
PendingIntent pi = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.build());
Toast.makeText(getApplicationContext(),"Notification showed!",Toast.LENGTH_LONG).show();
}
catch (Exception e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
原因可能是你的模拟器低于API级别26(奥利奥),您的手机,你所谈论的是大于或等于API级别26真正的设备,从奥利奥起,你需要以创建通知通道显示notifications.For的详细信息读取here.