Helo,我正在使用通知构建应用程序。我想在用户点击通知时将他发送到Fragment1。这几乎在所有情况下都没问题,除了一个。我的问题是当app只安装(从未启动)并且通知到来时,用户不会发送到Fragment1,然后他被发送到MainActivity ......我该怎么办?办?
这是myFirebaseMessagingService:
public class myFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From " + remoteMessage.getFrom());
Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage);
Log.d("Msg", "Poruka je stigla");
}
private void sendNotification(RemoteMessage remoteMessage){
Intent intent=new Intent(myFirebaseMessagingService.this, MainActivity.class);
intent.putExtra("action", "goToFragment1");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notification=new NotificationCompat.Builder(this)
.setSmallIcon(logo)
.setContentText(remoteMessage.getNotification().getBody())
.setContentTitle("Naslov")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification.build());
}
这是我的主要活动:
public class MainActivity extends AppCompatActivity {
Button dugme, dugme2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onCreate", "ONCREATE");
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String msg = getIntent().getStringExtra("action");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (msg != null) {
if (msg.equals("goToFragment1")) {
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.myFragment, fragment1);
Log.d("FragmentTransaction", "Fragment je promenjen u onCreate!");
fragmentTransaction.commit();
Log.d("Create", "Kraj onCreatea");
}
}
dugme = (Button) findViewById(R.id.dugme1);
dugme2 = (Button) findViewById(R.id.subscribe);
dugme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fragment = null;
if (view == dugme) {
fragment = new Fragment1();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.myFragment, fragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();
}
});
dugme2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseMessaging.getInstance().subscribeToTopic("android");
Log.d("Log", "Uspesno ste se pretplatili");
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d("onNewIntent", "NewIntent");
handleIntentExtra(intent);
}
private void handleIntentExtra(Intent intent) {
String msg = intent.getStringExtra("action");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (msg != null) {
if (msg.equals("goToFragment1")) {
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.myFragment, fragment1);
fragmentTransaction.commit();
}
}
}
答案 0 :(得分:0)
首次运行应用程序时,它将转到MainActivity中的onCreate方法。你有这两行代码 Fragment1 fragment1 = new Fragment1(); fragmentTransaction.replace(R.id.myFragment,fragment1);
试试这个: fragmentTransaction.add(R.id.myFragment,fragment1);
我建议将片段添加到后台。