android studio Benachrichtigungsklick: Aktivität ist bereits geöffnet
android studio label color (4)
Ich habe eine Anwendung mit Benachrichtigungen, die eine bestimmte Aktivität öffnen, wenn ich darauf klicke. Ich möchte das, wenn ich auf die Benachrichtigung klicke und die Aktivität bereits geöffnet ist, wird sie nicht erneut gestartet, sondern nur nach vorne gebracht.
Ich dachte, ich könnte es mit der Flagge FLAG_ACTIVITY_BROUGHT_TO_FRONT
oder FLAG_ACTIVITY_REORDER_TO_FRONT
, aber es öffnet es immer wieder, so dass ich die Aktivität zweimal habe.
Das ist mein Code:
event_notification = new Notification(R.drawable.icon,
mContext.getString(R.string.event_notif_message), System.currentTimeMillis());
Intent notificationIntent = new Intent(mContext, EventListActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
sendNotification(event_notification, notificationIntent, mContext.getString(R.string.event_notif_title),
body, Utils.PA_NOTIFICATIONS_ID);
Kann ich es mit Flags verwalten oder sollte ich eine Variable in SharedPreferences speichern, um zu prüfen, ob sie geöffnet ist oder nicht?
Vielen Dank!
https://code.i-harness.com
Ich denke, du solltest etwas Logik hinzufügen, damit es funktioniert, vielleicht kann das helfen:
Zum Beispiel habe ich einen Startbildschirm (Launcher und MAIN) von APP:
public class SplashScreen extends AppCompatActivity {
private final int TIME_OUT = 2000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// Suscribirse al tema Notificaciones
FirebaseMessaging.getInstance().subscribeToTopic("NOTA");
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().size()>1){
Intent home_activity = new Intent(getApplicationContext(), Home.class);
home_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = "" + getIntent().getExtras().getString(key);
Log.d("TAG", key + "=" + value);
switch (key) {
case "url":
home_activity.putExtra("url", value);
break;
}
}
}
startActivity(home_activity);
finish();
}else{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
Intent home_activity = new Intent(getApplicationContext(), Home.class);
home_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home_activity);
finish();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}, TIME_OUT);
}
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
Intent home_activity = new Intent(getApplicationContext(), Home.class);
home_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home_activity);
finish();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}, TIME_OUT);
}
}
}
Und in meinem FirebaseService habe ich Folgendes getan:
public class FCMessagingService extends FirebaseMessagingService {
private final String TAG = "PUSH";
private String body = "";
private static String _url = "";
private static int numMessage = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String from = remoteMessage.getFrom();
Log.d(TAG, "Mensaje recibido de: " + from);
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Notificación: " + remoteMessage.getNotification().getBody());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Data: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String url = data.getString("url");
Log.d(TAG, "onMessageReceived: \n" + "Extra Information: " + url);
this._url = url;
Log.d("_URL",_url);
mostrarNotificacion(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
mensaje(url, remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private void mensaje(String url, String title, String body){
boolean acti = Util.comprobarActivityALaVista(getApplicationContext(), "com.dev.android.subagan.MainActivity");
if(acti){
Intent imain = new Intent(MainActivity.URL);
imain.putExtra("key_url",url);
imain.putExtra("key_title",title);
imain.putExtra("key_body",body);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(imain);
}else{
Intent ihome = new Intent(Home.URL);
ihome.putExtra("key_url",url);
ihome.putExtra("key_title",title);
ihome.putExtra("key_body",body);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(ihome);
}
}
private void mostrarNotificacion(String title, String body) {
final int NOTIFICATION_ID = 3000;
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("url",_url);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT );
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setTicker(body)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Sie müssen das Attribut launchMode
für die Activity
launchMode
, mit der Sie singleTop
. Dies führt dazu, dass eingehende Intents an die vorhandene Instanz geliefert werden, anstatt eine neue Instanz zu starten, wenn sich diese Activity
bereits an der Spitze des Stacks der Task befindet.
Dies geschieht im Manifest, indem Sie android:launchMode="singleTop"
zum Element <activity>
hinzufügen. Wenn Sie auf die neueste Absicht zugreifen möchten (falls Sie an Daten interessiert sind, die möglicherweise mit ihr onNewIntent()
), überschreiben Sie onNewIntent()
in Ihrer Activity
.
Verwenden Sie onNewIntent()
um neue Daten aus der Benachrichtigung zu verarbeiten, klicken Sie auf und aktualisieren Sie die Aktivität.
In onNewIntent
holen onNewIntent
sich die neuen Daten aus der neuen Absicht (die durch die neue Benachrichtigung bedient wurde) und fangen Sie sie ab, zum Beispiel:
title = intent.getStringExtra("title")
in onCreate
vorher :)
Es aktualisiert die aktuelle Aktivität mit neuen Benachrichtigungsdaten.
Sie können auch dieses Tutorial folgen: http://androidrace.com/2016/12/10/how-to-refresh-activity-on-new-notification-click-android-developer/
Notification.Builder mBuilder =
new Notification.Builder(this)
.setSmallIcon(R.drawable.cmplayer)
.setContentTitle("CoderoMusicPlayer")
.setContentText("PLayer0!");
Intent resultIntent = new Intent(this,
AndroidBuildingMusicPlayerActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, 0);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
Kopieren Sie einfach den Code und fügen Sie ihn in Ihre Haupt-Launcher-Aktivität ein.
Hier ist die ursprüngliche Antwort