2016-06-10 28 views
1

我正在構建一個推送通知的應用程序。我想,當用戶點擊通知將他發送到Fragment1。但我想用Rxjava來做到這一點。怎麼做?從Rxjava通知到Fragment?

這裏是我的MainActivity:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d("onCreate", "ONCREATE"); 
    setContentView(R.layout.activity_main); 
    Intent intent = getIntent(); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    String msg = getIntent().getStringExtra("action"); 

    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 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 Fragment1(); 
      fragmentTransaction.replace(R.id.myFragment, fragment1); 
      Log.d("FragmentTransaction", "Fragment je promenjen!"); 
      fragmentTransaction.commit(); 
      Log.d("onResume", "Kraj resuma"); 
     } 
    } 
} 

這是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()); 



} 

而且,我想從發送通知用戶片段1適用於所有情況(當應用程序在前臺,當應用程序在後臺,當應用程序被殺害)

+0

開始閱讀關於RxJava,你不能指望人們重構你的代碼。在這裏你可以看到一些關於RXJava https:// github的最常用的例子。com/politrons/reactive – paul

回答

1

我看不出有理由在您的情況下使用RxJava。關於即使當你的應用程序在後臺時打開你的Fragment也很容易。

看看我的生產代碼做同樣的:

Intent intent = new Intent(Intents.ACTION_OPEN_ALBUM); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.putExtra("id", albumId); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.parseInt(albumId), intent, 0); 

pendingIntent.send(); 

礦之間的主要區別和你的是標誌。您正在使用FLAG_ACTIVITY_CLEAR_TOP期望在您的任務中運行一個MainActivity實例。

看看下面的文檔:

FLAG_ACTIVITY_CLEAR_TOP

如果集和正在啓動的 已經在當前任務運行,則代替開展該活動的一個新 實例的活動,所有的其他活動 將被關閉,並且這個意圖將作爲新的意圖被傳遞到(現在在頂部) 舊活動。

參考:https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

當你的應用程序沒有運行你沒有任務呢,所以你應該使用FLAG_ACTIVITY_NEW_TASK。

FLAG_ACTIVITY_NEW_TASK

如果設置,這個活動將成爲本屆 歷史堆棧上一個新任務的開始。

參考:https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK

您的代碼應該是這樣的:

private void sendNotification(RemoteMessage remoteMessage) { 
    Intent intent = new Intent("your.package.WHATEVER_ACTION"); 
    intent.putExtra("action", "goToFragment1"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

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

在你的清單,你應該設置的行動意圖過濾器:

<activity 
     android:name="your.package.YourActivity"> 

     <intent-filter> 
      <action android:name="your.package.WHATEVER_ACTION" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

    </activity> 

希望它可以幫助!

此致敬禮。

+0

好的,但我現在感到困惑..這個FLAG_ACTIVITY_NEW_TASK是應用程序沒有運行時,或者如果應用程序在後臺? –

+0

您應該只使用FLAG_ACTIVITY_NEW_TASK。它會打開你在歷史堆棧上的全新任務的活動 –

+0

我把這個你的旗幟,並再次發送給我的主要活動的第一頁,而不是片段1 –