Starting app only if its not currently running

后端 未结 11 1603
我寻月下人不归
我寻月下人不归 2020-12-02 23:07

I am sending push notification to users which when clicking on it opens the app.

My problem is that when the app is already open, clicking on the notification start

相关标签:
11条回答
  • 2020-12-02 23:43

    For those who use Xamarin.Android. The Xamarin version of David Wasser's answer is below:

            //Create notification
            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
            Intent uiIntent = PackageManager.GetLaunchIntentForPackage("com.company.app");
    
            //Create the notification
            var notification = new Notification(Android.Resource.Drawable.SymActionEmail, title);
    
            //Auto-cancel will remove the notification once the user touches it
            notification.Flags = NotificationFlags.AutoCancel;
    
            //Set the notification info
            //we use the pending intent, passing our ui intent over, which will get called
            //when the notification is tapped.
            notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, PendingIntentFlags.OneShot));
    
            //Show the notification
            notificationManager.Notify(0, notification);
    
    0 讨论(0)
  • try adding this to your intent to bring activity to front if it is running in the background

    Intent intent = new Intent(this, Splash.class); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

    0 讨论(0)
  • 2020-12-02 23:45

    when notification clicked and your code that redirect to your desire screen just replace that code by calling this method and redirect to particular screen on "true/false" result basis.

        private boolean isAppOnForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
          return false;
        }
        final String packageName = context.getPackageName();
        for (RunningAppProcessInfo appProcess : appProcesses) {
          if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
            return true;
          }
        }
        return false;
      }
    
    0 讨论(0)
  • 2020-12-02 23:51

    Use Splash as Fragment instead of Activity. Keep Splash fragment(7 seconds), replace the same with the desired one(landing page).

    Add launchMode="singleTask" to the manifest.

    As already stated by Rahul, onNewIntent() get called if application is already running else onCreate()

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

    OR

    Go with David's answer, seems promising.

    0 讨论(0)
  • 2020-12-02 23:57

    Use a "launch Intent" for your app, like this:

    PackageManager pm = getPackageManager();
    Intent launchIntent = pm.getLaunchIntentForPackage("your.package.name");
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, 0);
    

    Replace "your.package.name" with the name of your package from the Android manifest.

    Also, you should remove the special launchMode="singleTask" from your manifest. Standard Android behaviour will do what you want.

    0 讨论(0)
提交回复
热议问题