2016-11-07 67 views
0

我的应用程序包含一个textview,我试图使用来自Firebase的推送通知进行更新。它工作正常,当应用程序处于前景状态,但是,如果应用程序处于后台状态,我只收到通知,文本没有得到更新。Firebase推送通知不会同时更新文本

我的问题: -

想,如果我的应用程序在屏幕上,并且消息是从火力推,TextView的获得与在通知栏中没有通知更新。当应用程序处于后台状态时,我只收到通知,textview不会更新。

我想要什么?

是否可以更新textview以及接收通知而不考虑应用程序的状态?

MainActivity

public class MainActivity extends AppCompatActivity { 

private static final String TAG = MainActivity.class.getSimpleName(); 
private BroadcastReceiver mRegistrationBroadcastReceiver; 
private TextView txtRegId, txtMessage; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    txtRegId = (TextView) findViewById(R.id.txt_reg_id); 
    txtMessage = (TextView) findViewById(R.id.txt_push_message); 

    mRegistrationBroadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      // checking for type intent filter 
      if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) { 
       // gcm successfully registered 
       // now subscribe to `global` topic to receive app wide notifications 
       FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL); 

       displayFirebaseRegId(); 

      } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) { 
       // new push notification is received 

       String message = intent.getStringExtra("message"); 

       Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show(); 
       if (!TextUtils.isEmpty(message)) 
        txtMessage.setText("New Message: " + message); 
       else 
        txtMessage.setText("No new Updates!"); 
      } 
     } 
    }; 

    displayFirebaseRegId(); 

} 

// Fetches reg id from shared preferences 
// and displays on the screen 
private void displayFirebaseRegId() { 
    SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0); 
    String regId = pref.getString("regId", null); 

    Log.e(TAG, "Firebase reg id: " + regId); 

    if (!TextUtils.isEmpty(regId)) 
     txtRegId.setText("Firebase Reg Id: " + regId); 
    else 
     txtRegId.setText("Firebase Reg Id is not received yet!"); 
} 



@Override 
protected void onResume() { 
    super.onResume(); 

    // register GCM registration complete receiver 
    LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, 
      new IntentFilter(Config.REGISTRATION_COMPLETE)); 

    // register new push message receiver 
    // by doing this, the activity will be notified each time a new message arrives 
    LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, 
      new IntentFilter(Config.PUSH_NOTIFICATION)); 

    // clear the notification area when the app is opened 
    NotificationUtils.clearNotifications(getApplicationContext()); 
} 

@Override 
protected void onPause() { 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); 
    super.onPause(); 
} 
} 

FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = MyFirebaseMessagingService.class.getSimpleName(); 

private NotificationUtils notificationUtils; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Log.e(TAG, "From: " + remoteMessage.getFrom()); 

    if (remoteMessage == null) 
     return; 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); 
     handleNotification(remoteMessage.getNotification().getBody()); 
    } 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 

     try { 
      JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
      handleDataMessage(json); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
    } 
} 

private void handleNotification(String message) { 
    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
     // app is in foreground, broadcast the push message 
     Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
     pushNotification.putExtra("message", message); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 

     // play notification sound 
     NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
     notificationUtils.playNotificationSound(); 
    }else{ 
     // If the app is in background, firebase itself handles the notification 
    } 
} 

private void handleDataMessage(JSONObject json) { 
    Log.e(TAG, "push json: " + json.toString()); 

    try { 
     JSONObject data = json.getJSONObject("data"); 

     String title = data.getString("title"); 
     String message = data.getString("message"); 
     boolean isBackground = data.getBoolean("is_background"); 
     String imageUrl = data.getString("image"); 
     String timestamp = data.getString("timestamp"); 
     JSONObject payload = data.getJSONObject("payload"); 

     Log.e(TAG, "title: " + title); 
     Log.e(TAG, "message: " + message); 
     Log.e(TAG, "isBackground: " + isBackground); 
     Log.e(TAG, "payload: " + payload.toString()); 
     Log.e(TAG, "imageUrl: " + imageUrl); 
     Log.e(TAG, "timestamp: " + timestamp); 


     if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
      // app is in foreground, broadcast the push message 
      Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
      pushNotification.putExtra("message", message); 
      LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 

      // play notification sound 
      NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
      notificationUtils.playNotificationSound(); 
     } else { 
      // app is in background, show the notification in notification tray 
      Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); 
      resultIntent.putExtra("message", message); 

      // check for image attachment 
      if (TextUtils.isEmpty(imageUrl)) { 
       showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent); 
      } else { 
       // image is present, show notification with image 
       showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl); 
      } 
     } 
    } catch (JSONException e) { 
     Log.e(TAG, "Json Exception: " + e.getMessage()); 
    } catch (Exception e) { 
     Log.e(TAG, "Exception: " + e.getMessage()); 
    } 
} 

/** 
* Showing notification with text only 
*/ 
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) { 
    notificationUtils = new NotificationUtils(context); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent); 
} 

/** 
* Showing notification with text and image 
*/ 
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) { 
    notificationUtils = new NotificationUtils(context); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl); 
} 
} 

回答

0

你的问题不在于火力点,但与LocalBroadcastManager。 您正在销毁您的实例onPause,并在活动进入后台时发生。因此,从火力消息没有得到该Activity.Please变化:

@Override 
protected void onPause() { 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); 
    super.onPause(); 
} 

活动时被摧毁这就是所谓的而不是当活动是在后台

@Override 
protected void onDestroy() { 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); 
    super.onDestroy(); 
} 

希望这有助于 最好的!

+0

试过还是一样的结果:( –