我正在使用推送通知构建应用。我想,当用户点击通知将他发送到Fragment1时。但我想用Rxjava做到这一点。怎么做?
这是我的MainActivity:
df.index = df.Date + df.Heure
print (df)
NumeroM Date Heure Reg
2016-05-26 15:32:00 2396 2016-05-26 15:32:00 2396
2016-05-26 15:34:00 2397 2016-05-26 15:34:00 75599
2016-05-26 15:35:00 2398 2016-05-26 15:35:00 5822
2016-05-26 15:37:00 2399 2016-05-26 15:37:00 45225
2016-05-26 15:38:00 2400 2016-05-26 15:38:00 5266
这是myFirebaseMessagingService:
@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");
}
}
}
并且,我希望将用户从通知发送到fragment1以用于所有情况(当应用程序在前台时,当应用程序在后台并且应用程序被杀死时)
答案 0 :(得分:1)
我认为没有理由在你的情况下使用RxJava。关于打开你的片段,即使你的应用程序在后台,它也很容易。
看一下我的生产代码:
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>
希望它有所帮助!
祝你好运。