收到fcm通知时,我正试图打开IncomingCallActivity。我正在使用以下代码,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_incoming_call);
清单内
<activity android:name=".Activities.call.IncomingCallActivity"
android:exported="true">
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
在FirebaseMessagingService类中
public void onMessageReceived(RemoteMessage remoteMessage) {
System.out.println("http fcm message:: " + remoteMessage);
Intent intent = new Intent(getApplicationContext(), IncomingCallActivity.class);
IncomingCall incomingCall = new IncomingCall();
incomingCall.setJitsiToken(jsonObject.getString("jitsiToken"));
incomingCall.setUpdatedAt(jsonObject.getString("updatedAt"));
incomingCall.setDescription(jsonObject.getString("description"));
incomingCall.setProfilePicture(jsonObject.getString("profilePicture"));
incomingCall.setStatus(jsonObject.getString("status"));
incomingCall.setUserId(jsonObject.getString("userId"));
incomingCall.setUsername(jsonObject.getString("username"));
incomingCall.setUrl("https://" + jsonObject.getString("url"));
incomingCall.setType(jsonObject.getString("type"));
incomingCall.setGroup(jsonObject.getString("group"));
incomingCall.setVideo(jsonObject.getString("video"));
incomingCall.setCreatedAt(jsonObject.getString("createdAt"));
incomingCall.setFullName(jsonObject.getString("fullName"));
incomingCall.setUnreadNotification(jsonObject.getString("unreadNotification"));
incomingCall.setUnreadNotification(jsonObject.getString("unreadNotification"));
incomingCall.setNotificationType(jsonObject.getString("notificationType"));
incomingCall.setCallRoomId(jsonObject.getString("callRoomId"));
intent.putExtra("data", new Gson().toJson(incomingCall));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
但当应用程序处于后台或未打开时,这在android 11中不起作用。
是的,这是Android 10及更高版本的预期行为。来自文件:
Android 10(API 29级(和更高版本对何时应用程序设置限制可以在应用程序在后台运行时启动活动。由您应该显示通知(以提供紧急信息(。
但是,当您可以从后台启动活动时,它有一个例外列表。你可以在这里找到列表:
https://developer.android.com/guide/components/activities/background-starts#exceptions
也许列表中的一些例外情况适用于您的情况。
这对我有效。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + this.getPackageName()));
startActivityForResult(intent, 1);
} else {
//Permission Granted-System will work
}
}