如何在设备锁定屏幕上冷启动React Native应用程序?

xkftehaa  于 6个月前  发布在  React
关注(0)|答案(1)|浏览(80)

我正在React Native上构建一个VoIP应用程序,它使用推送通知来检测来电。我需要启动应用程序并在收到推送通知时将其置于前台。我可以在以下场景中实现这一点:

  • 当设备解锁并且:
  • 应用程序已最小化(仍在后台)
  • 应用程序不在后台(从多任务视图中删除)
  • 当设备被锁定并且:
  • 应用程序已最小化(仍在后台)

我唯一无法处理的情况是当设备被锁定,应用程序被杀死。应用程序启动,但不显示在锁定屏幕上。相反,用户需要解锁手机才能访问应用程序。
下面是收到通知时运行的代码,

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    // Check if app is running

    if(MainActivity.isAppRunning) {
        startActivity(notificationIntent);
        Intent messagingEvent = new Intent(MESSAGE_EVENT);
        messagingEvent.putExtra("message", remoteMessage);
        // Broadcast it so it is only available to the RN Application
        LocalBroadcastManager
                .getInstance(this)
                .sendBroadcast(messagingEvent);
    } else {
        startActivity(notificationIntent);
        try {
            // If the app is in the background we send it to the Headless JS Service
            Intent headlessIntent = new Intent(
                    this.getApplicationContext(),
                    BackgroundListenService.class
            );
            headlessIntent.putExtra("message", remoteMessage);
            this
                    .getApplicationContext()
                    .startService(headlessIntent);
            Log.d(TAG, "message: " + remoteMessage);
            HeadlessJsTaskService.acquireWakeLockNow(this.getApplicationContext());
        } catch (IllegalStateException ex) {
            Log.e(
                    TAG,
                    "Background messages will only work if the message priority is set to 'high'",
                    ex
            );
        }
    }
}

字符串
这是我的主要活动:

public class MainActivity extends NavigationActivity {

  public static boolean isAppRunning;
  private static boolean isMessageRecieved;

    private class MessageReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            isMessageRecieved=true;
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            window.clearFlags(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
        }
    }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);
    super.onCreate(savedInstanceState);
    isAppRunning = true;

      LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
      // Subscribe to message events
      localBroadcastManager.registerReceiver(
          new MainActivity.MessageReceiver(),
          new IntentFilter(MyFirebaseMessagingService.MESSAGE_EVENT)
      );

      if(isMessageRecieved) {
          Window window = getWindow();
          window.clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
          window.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
          window.clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
          window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
          window.clearFlags(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
      }

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    String channelId = "1";
    String channel2 = "2";

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      NotificationChannel notificationChannel = new NotificationChannel(channelId,
              "Channel 1",NotificationManager.IMPORTANCE_HIGH);

      notificationChannel.setDescription("This is BNT");
      notificationChannel.setLightColor(Color.RED);
      notificationChannel.enableVibration(true);
      notificationChannel.setShowBadge(true);
      notificationManager.createNotificationChannel(notificationChannel);

      NotificationChannel notificationChannel2 = new NotificationChannel(channel2,
              "Channel 2",NotificationManager.IMPORTANCE_MIN);

      notificationChannel.setDescription("This is bTV");
      notificationChannel.setLightColor(Color.RED);
      notificationChannel.enableVibration(true);
      notificationChannel.setShowBadge(true);
      notificationManager.createNotificationChannel(notificationChannel2);

    } 
  }
   @Override
    protected void onDestroy() {
        super.onDestroy();
        isAppRunning = false;
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

}

kgsdhlau

kgsdhlau1#

在AndroidManifest.xml文件中,您需要设置:

<activity android:name=".MainActivity"
    ...
    android:showWhenLocked="true"
    android:turnScreenOn="true"
>

字符串
也许这种许可

<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

相关问题