Android 活动未自动从后台启动
在后台收到firebase推送通知后,我尝试自动启动Android 活动未自动从后台启动,android,flutter,broadcastreceiver,android-service,Android,Flutter,Broadcastreceiver,Android Service,在后台收到firebase推送通知后,我尝试自动启动MainActivity,但活动未启动。这是我的密码: firebase推送数据有效负载: { "data": { "title":"this is push title", "body": "this is push body", "click_action": "FLUTTER_NOTIFICATION_CLICK" }, "to": "" } @Ove
MainActivity
,但活动未启动。这是我的密码:
firebase推送数据有效负载:
{
"data": {
"title":"this is push title",
"body": "this is push body",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"to": ""
}
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
if (isApplicationForeground(this)) {
Intent intent = new Intent(ACTION_REMOTE_MESSAGE);
intent.putExtra(EXTRA_REMOTE_MESSAGE, remoteMessage);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
else {
notification(backgroundContext, remoteMessage);
}
}
private void notification(Context context, RemoteMessage remoteMessage) {
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent
.getActivity(context, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String channelId = context.getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
if (notificationManager != null) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(context, ActivityOpener.class);
pendingIntent = PendingIntent.getBroadcast(context,0, myIntent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), pendingIntent);
}
}
public class ActivityOpener extends BroadcastReceiver {
private static final String TAG = "ActivityOpener";
@Override
public void onReceive(Context context, Intent intent) {
Intent wakeIntent = new Intent(context, MainActivity.class);
wakeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(wakeIntent);
Log.d(TAG, "activity opener");
}
}
FireBaseMessingService-onMessageReceived方法:
{
"data": {
"title":"this is push title",
"body": "this is push body",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"to": ""
}
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
if (isApplicationForeground(this)) {
Intent intent = new Intent(ACTION_REMOTE_MESSAGE);
intent.putExtra(EXTRA_REMOTE_MESSAGE, remoteMessage);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
else {
notification(backgroundContext, remoteMessage);
}
}
private void notification(Context context, RemoteMessage remoteMessage) {
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent
.getActivity(context, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String channelId = context.getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
if (notificationManager != null) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(context, ActivityOpener.class);
pendingIntent = PendingIntent.getBroadcast(context,0, myIntent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), pendingIntent);
}
}
public class ActivityOpener extends BroadcastReceiver {
private static final String TAG = "ActivityOpener";
@Override
public void onReceive(Context context, Intent intent) {
Intent wakeIntent = new Intent(context, MainActivity.class);
wakeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(wakeIntent);
Log.d(TAG, "activity opener");
}
}
活动开场白广播接收器:
{
"data": {
"title":"this is push title",
"body": "this is push body",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"to": ""
}
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
if (isApplicationForeground(this)) {
Intent intent = new Intent(ACTION_REMOTE_MESSAGE);
intent.putExtra(EXTRA_REMOTE_MESSAGE, remoteMessage);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
else {
notification(backgroundContext, remoteMessage);
}
}
private void notification(Context context, RemoteMessage remoteMessage) {
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent
.getActivity(context, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String channelId = context.getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
if (notificationManager != null) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(context, ActivityOpener.class);
pendingIntent = PendingIntent.getBroadcast(context,0, myIntent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), pendingIntent);
}
}
public class ActivityOpener extends BroadcastReceiver {
private static final String TAG = "ActivityOpener";
@Override
public void onReceive(Context context, Intent intent) {
Intent wakeIntent = new Intent(context, MainActivity.class);
wakeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(wakeIntent);
Log.d(TAG, "activity opener");
}
}
此广播接收器
在推送后在后台触发,但活动未打开。我当前的设备操作系统版本Android 9
我有什么遗漏吗?