在 android 中关闭应用程序时不显示推送通知
Push notification not displayed when app is closed in android
我有两台设备。一个是 运行 API 26,另一个是 API 21。我正在使用 Googles firebase 进行通知。 API 26 设备不会收到推送通知,除非我先打开应用程序。然后它收到一切正常。我不必先打开应用程序即可在 API 27 设备上接收推送通知。关于如何在不打开应用程序的情况下始终接收推送的任何想法?我希望只需打开用户的 phone.
即可将通知发送给用户
private void sendNotification(String title, String messageBody, String clickAction) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "";
if (Build.VERSION.SDK_INT >= 26) {
id = "my_channel_01";
// The user-visible name of the channel.
CharSequence name = "myChannel";
// The user-visible description of the channel.
String description = "";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name,importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(mChannel);
}
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());
}
在清单文件中添加 BootBroadcastReceiver
<receiver
android:name=".OnBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
创建以下 Java 文件。在其中调用 FirebaseMessagingReceiveService。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.demo.FirebaseMessagingReceiveService");
i.setClass(context, FirebaseMessagingReceiveService.class);
context.startService(i);
}
}
我有两台设备。一个是 运行 API 26,另一个是 API 21。我正在使用 Googles firebase 进行通知。 API 26 设备不会收到推送通知,除非我先打开应用程序。然后它收到一切正常。我不必先打开应用程序即可在 API 27 设备上接收推送通知。关于如何在不打开应用程序的情况下始终接收推送的任何想法?我希望只需打开用户的 phone.
即可将通知发送给用户private void sendNotification(String title, String messageBody, String clickAction) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "";
if (Build.VERSION.SDK_INT >= 26) {
id = "my_channel_01";
// The user-visible name of the channel.
CharSequence name = "myChannel";
// The user-visible description of the channel.
String description = "";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name,importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(mChannel);
}
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());
}
在清单文件中添加 BootBroadcastReceiver
<receiver
android:name=".OnBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
创建以下 Java 文件。在其中调用 FirebaseMessagingReceiveService。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.demo.FirebaseMessagingReceiveService");
i.setClass(context, FirebaseMessagingReceiveService.class);
context.startService(i);
}
}