【问题标题】:Android 10 - Activity is not opening when App is in background and in lock screenAndroid 10 - 当应用程序处于后台和锁定屏幕时,活动未打开
【发布时间】:2020-02-03 11:34:59
【问题描述】:

我正在使用 One plus 设备 (Android 10) 中的通话应用程序当我使用 twilio 从一个用户向另一个用户拨打电话时,我在应用程序处于后台时收到来电通知,然后我开始来电使用传入活动的屏幕,但在一加中它不起作用。在 Android 10 以下的其他设备上,它可以正常工作。

@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
    Intent intent = new Intent(this, IncomingCallActivity.class);
    intent.setAction(IncomingCallActivity.ACTION_INCOMING_CALL);
    intent.putExtra(IncomingCallActivity.INCOMING_CALL_NOTIFICATION_ID, notificationId);
    intent.putExtra(IncomingCallActivity.INCOMING_CALL_INVITE, callInvite);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    MyFirebaseMessagingService.this.startActivity(intent);
}

我也尝试过为活动添加标志

Window window = this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);


<activity
    android:name=".IncomingCallActivity"
    android:excludeFromRecents="true"
    android:noHistory="true"
    android:screenOrientation="sensorPortrait"
    android:showOnLockScreen="true"
    android:showWhenLocked="true"
    android:turnScreenOn="true" />

【问题讨论】:

  • 我在here 中回答了同样的问题。也许这会对你有所帮助。
  • @MuhammadFarhan 我知道你的回答。我收到通知,当我单击该通知时,我的通话屏幕正在打开。但同时What's app无需点击通知即可打开屏幕
  • 所以您收到通知时没有唤醒设备?
  • 收到通知但未打开屏幕时设备正在唤醒
  • 显示对时间敏感的通知 在 Android 10 中应用处于锁定模式时对我有效。在我重新安装应用后有效。

标签: android push-notification background firebase-cloud-messaging twilio-programmable-voice


【解决方案1】:

您需要使用包含您的活动的pendingintent 调用通知。 我从这里得到了代码: kraigsandroidalarm

 Intent notify = new Intent(this, AlarmNotificationActivity.class)
  .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

final NotificationManager manager =
  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
    manager.getNotificationChannel(FIRING_ALARM_NOTIFICATION_CHAN) == null) {
  // Create a notification channel on first use.
  NotificationChannel chan = new NotificationChannel(
      FIRING_ALARM_NOTIFICATION_CHAN,
      getString(R.string.ringing_alarm_notification),
      NotificationManager.IMPORTANCE_HIGH);
  chan.setSound(null, null);  // Service manages its own sound.
  manager.createNotificationChannel(chan);
}
final Notification notification =
  (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
   new Notification.Builder(this, FIRING_ALARM_NOTIFICATION_CHAN) :
   new Notification.Builder(this))
  .setContentTitle(getString(R.string.app_name))
  .setContentText(labels.isEmpty() ? getString(R.string.dismiss) : labels)
  .setSmallIcon(R.drawable.ic_alarm_on)
  // NOTE: This takes the place of the window attribute
  // FLAG_SHOW_WHEN_LOCKED in the activity itself for newer APIs.
  .setFullScreenIntent(PendingIntent.getActivity(this, 0, notify, 0), true)
  .setCategory(Notification.CATEGORY_ALARM)
  .setPriority(Notification.PRIORITY_MAX)
  .setVisibility(Notification.VISIBILITY_PUBLIC)
  .setOngoing(true)
  .setLights(Color.WHITE, 1000, 1000)
  .build();
notification.flags |= Notification.FLAG_INSISTENT;  // Loop sound/vib/blink
startForeground(FIRING_ALARM_NOTIFICATION_ID, notification);

CountdownRefresh.stop(getApplicationContext());

// NOTE: As of API 29, this only works when the app is in the foreground.
// https://developer.android.com/guide/components/activities/background-starts
// The setFullScreenIntent option above handles the lock screen case.
startActivity(notify);

看起来他调用了 startforeground 和 startactivity。 在我的应用程序中,我的活动被调用了两次,所以我只调用了 startforeground,但两次调用都可以正常工作。

【讨论】:

  • 链接不再有效:​​(
  • 谢谢你,我更新了,现在又可以工作了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
相关资源
最近更新 更多