gpt4 book ai didi

java - 当通知后应用程序未在堆栈上打开时,单击在 Android 中不起作用

转载 作者:行者123 更新时间:2023-12-01 18:28:17 26 4
gpt4 key购买 nike

enter image description here

类路径“com.google.gms:google-services:4.3.3”

添加依赖项

 implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-core:17.2.2'
implementation 'com.google.firebase:firebase-crash:16.2.1'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
implementation 'com.google.firebase:firebase-analytics:17.2.2'

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.gms:play-services-tagmanager:17.0.0'

MyFirebaseInstanceIDService.java

公共(public)类 MyFirebaseInstanceIDService 扩展了 FirebaseMessagingService {

private static final String TAG = "MyFirebaseIIDService";
public static String fcm_Tocken ;


@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);

storeRegIdInPref(s);

// sending reg id to your server
sendRegistrationToServer(s);

// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(CONSTANTS.REGISTRATION_COMPLETE);
registrationComplete.putExtra("token", s);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);


String refreshedToken = FirebaseInstanceId.getInstance().getToken();
FirebaseMessaging.getInstance().subscribeToTopic("all");
SharedPreferences.Editor editor1 = getSharedPreferences(CONSTANTS.API_param_DeviceToken, MODE_PRIVATE).edit();
editor1.putString(CONSTANTS.API_param_DeviceToken,refreshedToken); //Friend
editor1.apply();
editor1.commit();
fcm_Tocken = refreshedToken;
}
private void sendRegistrationToServer(final String token) {
// sending gcm token to server
Log.e(TAG, "sendRegistrationToServer: " + token);
}

private void storeRegIdInPref(String token) {
SharedPreferences.Editor editor1 = getSharedPreferences(CONSTANTS.API_param_DeviceToken, MODE_PRIVATE).edit();
editor1.putString(CONSTANTS.API_param_DeviceToken,token); //Friend
editor1.apply();
editor1.commit();
}

}

MyFirebaseMessagingService.java

公共(public)类 MyFirebaseMessagingService 扩展了 FirebaseMessagingService {

public static final String NOTIFICATION_CHANNEL_ID = "10001";
private NotificationManager mNotificationManager;
private NotificationCompat.Builder notificationBuilder;
Activity context;
String title = "", image = "", message = "", flag = "", id = "";

private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

FirebaseMessaging.getInstance().subscribeToTopic("topic");

Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
if (remoteMessage == null)
return;
if (remoteMessage.getNotification() != null) {

Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
title = remoteMessage.getNotification().getTitle();
message = remoteMessage.getNotification().getBody();
sendNotification(title, message, flag, id, String.valueOf(m));
}
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

try {
title = remoteMessage.getData().get("title");
image = remoteMessage.getData().get("image");
message = remoteMessage.getData().get("body");
flag = remoteMessage.getData().get("flag");
id = remoteMessage.getData().get("id");
sendNotification(title, message, flag, id, String.valueOf(m));

} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}

private void sendNotification(String title, String message, String flag, String id, String m) {

Intent resultIntent= null;
PendingIntent resultPendingIntent=null;
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
try {
if (flag != null && flag.equalsIgnoreCase("refer_friend")) {
resultIntent = new Intent(this, SquareEarnActivity.class);
resultIntent.putExtra(CONSTANTS.back_flag, CONSTANTS.FLAG_ONE);
taskStackBuilder.addParentStack(NavigationActivity.class);
taskStackBuilder.addNextIntentWithParentStack(resultIntent);
resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}else
resultIntent = new Intent(this, NavigationActivity.class);
taskStackBuilder.addParentStack(NavigationActivity.class);
taskStackBuilder.addNextIntentWithParentStack(resultIntent);
resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

NotificationChannel channel = new NotificationChannel("ID", "NAME", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Notification");
notificationManager.createNotificationChannel(channel);

notificationBuilder = new NotificationCompat.Builder(this, channel.getId());
} else {
notificationBuilder = new NotificationCompat.Builder(this);
}

notificationBuilder.setSmallIcon(R.drawable.logo);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL);
notificationBuilder.setColor(getResources().getColor(R.color.darkorange));
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
notificationBuilder.setContentIntent(resultPendingIntent);

Notification note = notificationBuilder.build();
note.flags = Notification.FLAG_INSISTENT;
note.flags = Notification.DEFAULT_VIBRATE;
note.flags = Notification.DEFAULT_SOUND;
note.flags = Notification.DEFAULT_LIGHTS;
note.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(Integer.parseInt(m), notificationBuilder.build());

} catch (Exception e) {
e.printStackTrace();
}
}

}

最佳答案

我认为问题出在 else 条件的下面一行

resultIntent = new Intent(this, NavigationActivity.class);
taskStackBuilder.addParentStack(NavigationActivity.class);
taskStackBuilder.addNextIntentWithParentStack(resultIntent);

您正在设置Parent并在通知点击时打开 Activity 相同。因此它不遵循 TaskStackBuilder 的返回堆栈架构。

您可以纠正家长并尝试一下,希望这会起作用。

关于java - 当通知后应用程序未在堆栈上打开时,单击在 Android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60200682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com