0

我正在開發一個應用程序,當用戶點擊推送通知時,在MainActivity上向他發送片段(我稱他爲FragmentSRB)。當應用程序處於前臺或後臺時,一切都可以。但是當我殺了應用程序,然後收到通知時,通知就會將我發送給主要活動,而不是FragmentSRB。我該怎麼辦?從第一次啓動應用程序之前的通知到片段?

這裏是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", "goToFragmentSRB"); 
    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()); 



} 

而且我mainActivity:

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 FragmentSRB(); 
      fragmentTransaction.replace(R.id.myFragment, fragmentSRB); 
      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"); 
    String msg = getIntent().getStringExtra("action"); 
    Log.d("Msg", "msg"); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    if (msg != null) { 
     if (msg.equals("goToFragment1")) { 
      Fragment1 fragment1 = new FragmentSRB(); 
      fragmentTransaction.replace(R.id.myFragment, fragmentSRB); 
      fragmentTransaction.commit(); 
     } 
    } 
} 


@Override 
protected void onResume() { 
    super.onResume(); 
    Log.d("onResume", "Resume"); 
    Intent intent = new Intent(); 
    String msg = getIntent().getStringExtra("action"); 
    Log.d("msg", "msg"); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    if (msg != null) { 
     if (msg.equals("goToFragment1")) { 
      Fragment1 fragment1 = new FragmentSRB(); 
      fragmentTransaction.replace(R.id.myFragment, fragmentSRB); 
      Log.d("FragmentTransaction", "Fragment je promenjen!"); 
      fragmentTransaction.commit(); 
      Log.d("onResume", "Kraj resuma"); 
     } 
    } 
} 
+0

請參考有關@覆蓋 保護無效onNewIntent(意向意圖){}。 onNewIntent()意味着已經在堆棧中的其他地方運行的singleTop活動的入口點,因此無法調用onCreate() – darwin

+0

因此,我該怎麼做?要刪除onNewIntent或其他內容,你有什麼想法? –

+0

在您的活動中重寫onNewIntent(),並將您的邏輯替換爲bot oncreate()和onNewIntent()中的片段。 – darwin

回答

0
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(); 
     handleIntentExtra(intent); 

     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); 
     handleIntentExtra(intent); 
    } 

    private void handleIntentExtra(Intent intent) { 
     String msg = intent.getStringExtra("action"); 
     Log.d("Msg", "msg"); 
     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

對不起隊友,我沒有寫出確切的問題。當應用程序第一次啓動時,我可以去分段,但是當我殺了應用程序,並且我收到通知(並且我的應用程序尚未啓動)時,則我轉到mainActivity,而不是FragmentSRB ...我希望你已經理解我.. –