代码之家  ›  专栏  ›  技术社区  ›  Magobin

从服务启动的通知不会在单击时打开应用程序

  •  2
  • Magobin  · 技术社区  · 7 年前

    早上好

    我已经做了一个应用程序来实现GpS定位。我有一个服务可以在LocationChanged事件中保存我的位置。为了避免Android关闭我的应用程序,我启动了一个通知,一切正常。但现在,我希望当我点击操作栏上的通知时,应用程序会回到前台。我使用fragment,映射片段称为MappaFragment。我读了很多信息,但似乎没有解决我的问题。下面是我的代码,任何建议都将不胜感激! 亚历克斯 这是我的监控服务:

        public int onStartCommand(Intent intent, int flags, int startId) {
    
            if (intent.getAction().equals(Constants.STARTFOREGROUND_ACTION)) {
                Log.i(TAG, "Received Start Foreground Intent ");
                Intent notificationIntent = new Intent(getApplicationContext(), LocationMonitoringService.class);
    
                Bundle extras = intent.getExtras();
    
                numeroTraccia = extras.getInt("numeroTraccia");
    
                itinerario = extras.getString("itinerarioRiferimento");
    
                notificationIntent.setAction(Constants.MAIN_ACTION);
    
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                Intent.FLAG_ACTIVITY_CLEAR_TASK);
    
                setGiornoRiferimento(gg.checkDayLong(this));
    
                PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
                    0, new Intent(getBaseContext(),MappaFragment.class), 0);
                Bitmap icon =  BitmapFactory.decodeResource(getResources(), com.example.alex.myApp.R.drawable.smallicon );
    
                Notification.BigTextStyle bigText = new Notification.BigTextStyle();
    
                Notification notification = new Notification.Builder(getApplicationContext())
                    .setContentTitle("MyApp")
                    .setTicker(getString(R.string.track))
                    .setStyle(bigText.bigText(getString(R.string.track)))
                    .setSmallIcon(R.drawable.smallicon)
                    .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                    .setContentIntent(pendingIntent)
                    .setOngoing(true)
                    .build();
             startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
                    notification);
    

    }

    这是我的清单:

        <activity android:name="com.example.alex.myApp.MainActivity"
            android:screenOrientation="portrait"
        android:configChanges="orientation|screenSize|keyboardHidden" />
    
        <activity
            android:name="com.example.alex.myApp.SplashScreen"
            android:label="@string/app_name">
    
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="com.example.alex.myApp.services.MonitoringService"
            android:enabled="true"/>
        <receiver
            android:name="com.example.alex.myApp.RestarterBroadcastReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="RestartWhenStopped">
            <intent-filter>
                <action android:name="com.example.alex.myApp.ActivityRecognition.RestartSensor"/>
            </intent-filter>
        </receiver>
    

    提前感谢! 亚历克斯

    2 回复  |  直到 7 年前
        1
  •  2
  •   Srushti    7 年前

    首先

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getBaseContext(),MappaFragment.class), 0);
    

    这是不正确的。无法使用intent打开片段。您需要启动一个活动,然后重定向到所需的片段。

    接下来,如果应用程序在后台,您单击通知,它将打开启动器活动。所以,在启动器活动中,您需要检查bundle对象,然后可以重定向到特定的活动或片段。 查看此帖子: Push notification works incorrectly when app is on background or not running Open specific Activity when notification clicked in FCM 它将帮助您更好地了解FCM在应用程序处于后台和前台时的工作方式。

        2
  •  0
  •   Shahin    7 年前

    创建如下广播接收器:

    public static class ActionReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
    
        String action = intent.getAction();
        if (action.equals("open_activity")) {
          intent.setAction(Intent.ACTION_MAIN);
          intent.addCategory(Intent.CATEGORY_LAUNCHER);
          intent.setClass(AppContext.getContext(), MappaFragment.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          context.startActivity(intent);
        }
      }
    }

    然后,用这个 notificationIntent.setAction("open_activity");

    并将悬挂式帐篷更改为:

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
                    0, new Intent(AppContext.getContext(), ActionReceiver.class), 0);
    

    注意:要使用此选项,需要删除以下行:

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification)
    

    我的建议是使用BroadcastReceiver来接收您想要在通知中执行的任何操作。

    此外,如果这不起作用,请尝试添加 .setAction(Intent.ACTION_MAIN) 通知如下:

    Notification notification = new Notification.Builder(getApplicationContext())
                    .setContentTitle("MyApp")
                    .setTicker(getString(R.string.track))
                    .setStyle(bigText.bigText(getString(R.string.track)))
                    .setSmallIcon(R.drawable.smallicon)
                    .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                    .setContentIntent(pendingIntent)
                    .setOngoing(true)
                    .setAction("open_activity")
                    .build();