这是我的代码:它的功能很棒,但是在收到通知时启动了应用程序。我只是不想那样。我想收到通知,但不启动应用程序。
Intent intent2 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(context,"default")
.setSmallIcon(R.drawable.orgnic_farm)
.setContentTitle("EFarm Says!")
.setContentText("Hey!, Your Order will be Delivered to you Today.")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManager notificationManagerCompat= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("com.myApp");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"com.myApp",
"My App",
NotificationManager.IMPORTANCE_DEFAULT
);
if (notificationManagerCompat != null) {
notificationManagerCompat.createNotificationChannel(channel);
}
}
notificationManagerCompat.notify(100,builder.build());
根据您的代码,仅在点击通知时将启动您的应用程序,并显示MainActivity。如果您收到通知,它将不会自动启动该应用程序。
[如果您不希望在点击通知时启动应用程序,则只需删除行setContentIntent()
。