从服务启动的通知不会在单击时打开应用程序
Notification started from service doesn't open the app on click
早上好,
我制作了一个实现 GpS 定位的应用程序。我有一项服务可以在 LocationChanged 事件中保存我的位置。为了避免 Android 关闭我的应用程序,我启动了一个通知并且一切正常。但是现在,我希望当我从 Action Bar 单击通知时,应用程序返回 foreground.I 使用片段并且地图片段称为 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>
提前致谢!
亚历克斯
像下面这样创建广播接收器:
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 = 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();
首先,
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getBaseContext(),MappaFragment.class), 0);
这是不正确的。无法使用意图打开片段。您需要启动一个 activity,然后重定向到所需的片段。
接下来是,如果应用程序在后台并且您单击通知,它将打开启动器 activity。所以在启动器 activity 中你需要检查 bundle 对象,然后你可以重定向到特定的 activity 或片段。
检查此 post: and Open specific Activity when notification clicked in FCM 它将帮助您更好地了解 FCM 在应用程序处于后台和前台时的工作方式。
早上好,
我制作了一个实现 GpS 定位的应用程序。我有一项服务可以在 LocationChanged 事件中保存我的位置。为了避免 Android 关闭我的应用程序,我启动了一个通知并且一切正常。但是现在,我希望当我从 Action Bar 单击通知时,应用程序返回 foreground.I 使用片段并且地图片段称为 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>
提前致谢! 亚历克斯
像下面这样创建广播接收器:
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 = 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();
首先,
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getBaseContext(),MappaFragment.class), 0);
这是不正确的。无法使用意图打开片段。您需要启动一个 activity,然后重定向到所需的片段。
接下来是,如果应用程序在后台并且您单击通知,它将打开启动器 activity。所以在启动器 activity 中你需要检查 bundle 对象,然后你可以重定向到特定的 activity 或片段。
检查此 post: