2017-03-25 22 views
0

我正在使用Firebase通知服务。当我从Firebase发送通知时。弹出窗口必须仅在Android中触发应用程序前台

我不想保存这些信息。我的意思是通知仅在用户在MainActivity中联机时显示。如果用户运行另一个活动,他看不到任何通知,但是当用户来到MainActiviy通知时必须到来。

public class NotificationService extends FirebaseMessagingService { 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    super.onMessageReceived(remoteMessage); 
    String getBody = remoteMessage.getNotification().getBody(); 
    // Uygulama suan kullanılıyorsa. 
    if (isAppForground(getApplicationContext())){ 
     getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class) 
       .putExtra("data",getBody) 
       .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 
    }else{ 

     Notification notification = new NotificationCompat.Builder(this) 
       .setContentTitle("Fixtown") 
       .setContentText(getBody) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .build(); 
     NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext()); 
     manager.notify(123, notification); 
    } 

} 

public boolean isAppForground(Context mContext) { 

    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); 
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); 
    if (!tasks.isEmpty()) { 
     ComponentName topActivity = tasks.get(0).topActivity; 
     if (!topActivity.getPackageName().equals(mContext.getPackageName())) { 
      return false; 
     } 
    } 

    return true; 
} 

如你所见。那么哪个标志必须放入.setFlags()以及如何仅在MainActivity中捕获此通知。

回答

2

由于您希望让应用程序处理推送通知而不是系统,因此您希望使用数据消息而不是通知消息。 请参考: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

我会建议使用EventBus实现这一目标。

为了控制根据哪个活动在前台弹出的通知,您需要在onResume()上注册并在onPause()上取消注册。

在你的应用程序级的build.gradle:

compile 'org.greenrobot:eventbus:3.0.0' 

创建一个Java类,并将其命名为NotificationEvent:

public class NotificationEvent { 
    private String body; 

    public NotificationEvent(String body) { 
     this.body = body; 
    } 

    public String getBody() { 
     return body; 
    } 

    public void setBody(String body) { 
     this.body = body; 
    } 
} 

在你NotificationService:

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Log.d(TAG, "onMessageReceived"); 
    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     // do nothing if Notification message is received 
    } 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
     String body = remoteMessage.getData().get("body"); 
     EventBus.getDefault().post(new NotificationEvent(body)); 
    } 
} 

在您的MainActivity。 java:

@Override 
protected void onResume() { 
    super.onResume(); 
    EventBus.getDefault().register(this); 
} 

@Override 
protected void onPause() { 
    EventBus.getDefault().unregister(this); 
    super.onStop(); 
} 

@Subscribe 
public void onEvent(NotificationEvent event) { 
    // do something to the notification 
    Notification notification = new NotificationCompat.Builder(this) 
      .setContentTitle("Fixtown") 
      .setContentText(event.getBody()) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .build(); 
    NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext()); 
    manager.notify(123, notification); 
} 
+1

谢谢诺埃尔先生 – TeyteyLan

相关问题