未显示通知

时间:2019-04-01 09:00:31

标签: android notifications

我无法发出这个简单的通知,我一直在寻找教程,它们都提供相同的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("hgfyjdgh", "Mychannel", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

public void click(View v) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "id");
    builder.setSmallIcon(R.drawable.img)
            .setContentTitle("Uber Driver")
            .setContentText("أنت الان متصل")
            .setPriority(NotificationCompat.PRIORITY_HIGH);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(23, builder.build());
    Toast.makeText(this, "done", Toast.LENGTH_LONG).show();

}

1 个答案:

答案 0 :(得分:1)

NotificationChannel创建和通知时,您需要具有相同的频道

创建通知频道名称

String NOTIFICATION_CHANNEL_ID = "com.appname.example";

注册

NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "MyChannelName", NotificationManager.IMPORTANCE_DEFAULT);

通知

notificationManager.notify(NOTIFICATION_CHANNEL_ID, builder.build());