3

我的代碼集成FCM .. 我想獲得當前的活動上下文推送到達時。 用於通過上下文投送監聽器的目的。在Android中如何在擴展FirebaseMessagingService時調用onMessageReceived獲取當前的Activity上下文?

片段代碼在這裏...

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFMService"; 
private NotificationListener notificationListener; 
private Context context; 
private int count=0; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Log.d(TAG, "FCM Message Id: " + remoteMessage.getMessageId()); 

    RemoteMessage.Notification notification = remoteMessage.getNotification(); 
    Map<String, String> data = remoteMessage.getData(); 
    Log.e("FROM", remoteMessage.getFrom()); 

    count++; 

    //sendNotification(notification, data); 

    setNotificationCount(); 
} 


private void setNotificationCount(AlertList alertList) { 
    notificationListener = (NotificationListener) context; 
    notificationListener.onNotificationMessage(count); 
} 

private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) { 
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); 

    Intent intent = new Intent(this, AlertOnMap.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("AlertDetails", (Serializable) alertList); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
      .setContentTitle(notification.getTitle()) 
      .setContentText(notification.getBody()) 
      .setAutoCancel(true) 
      .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
      .setContentIntent(pendingIntent) 
      .setContentInfo(notification.getTitle()) 
      .setLargeIcon(icon) 
      .setColor(Color.RED) 
      .setSmallIcon(R.mipmap.ic_launcher); 

    try { 
     String picture_url = data.get("picture_url"); 
     if (picture_url != null && !"".equals(picture_url)) { 
      URL url = new URL(picture_url); 
      Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      notificationBuilder.setStyle(
        new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody()) 
      ); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0, notificationBuilder.build()); 
} 

}

創建界面

public interface NotificationListener { 

public void onNotificationMessage(AlertList alertList, int i); 

}

調用另一個class..like

public class Header extends AppCompatActivity implements NotificationListener{ 

/*--------------- OnNotification ----------------------*/ 

@Override 
public void onNotificationMessage(final int count) { 

    Log.d("Notification count", "---> In Header Count = " + count); 

} 

}

我想在沒有任何其他類的上下文引用的情況下獲取當前活動上下文。

+0

通常標誌着一個答案。新年快樂:) – Ewoks

回答

4

如果我理解正確,基本上你想要將活動的上下文轉換爲接口實現的活動。問題是:您無法獲取當前的活動上下文,因爲可能/根本沒有當前的活動。有一些上下文可用,但是這個上下文不能用於你想要的。讓我解釋一下:

比方說,用戶沒有使用應用程序一個星期,他完成了所有活動,退出應用程序(或操作系統釋放內存和清理所有內容)。服務仍在後臺運行,當推送消息到來時,它會根據您的代碼做出反應(假設您立即想要顯示通知)。如果你需要的上下文在這一刻,例如用於通知圖標的顏色,你可以使用以下方面:

int color = ContextCompat.getColor(getApplicationContext(), R.color.colorAccent); 

getApplicationContext()會給你需要的上下文,但是那是當然不是活動場景的,因爲有仍然沒有活動(所有人完成日前

希望這個解釋有助於理解這裏的概念。 的理解更好什麼樣的背景下存在檢查Context, What Context?

+0

是的,但我使用'getApplicationContext()'在我的情況下,但沒有任何結果與此。 - @ Ewoks –

+0

..在我的回答中,我解釋了爲什麼你沒有辦法做你想做的事:) – Ewoks

1

我做到了,從這樣

@Override 
public void onMessageReceived(final RemoteMessage remoteMessage) { 

    if (Manager.getInstance().actualContext != null) 
    { 
     //Let this be the code in your n'th level thread from main UI thread 
     Handler h = new Handler(Looper.getMainLooper()); 
     h.post(new Runnable() { 
      public void run() 
      { 
       CFAlertDialog.Builder pDialog = new CFAlertDialog.Builder(Manager.getInstance().actualContext) 
         .setDialogStyle(CFAlertDialog.CFAlertStyle.NOTIFICATION) 
         .setTextGravity(Gravity.CENTER) 
         .setMessage(remoteMessage.getNotification().getBody()) 
         .setTitle(remoteMessage.getNotification().getTitle()); 

       pDialog.show(); 

      } 
     }); 
    } 

    super.onMessageReceived(remoteMessage); 
} 

Manager.getInstance()。actualContext是我的經理變量上下文(Singletone類)

只要你必須在所有的活動

@Override 
protected void onResume() { 
    super.onResume(); 
    Manager.getInstance().actualContext = this; 
} 

,毀壞所接受,使別人更容易找到它,它

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    Manager.getInstance().actualContext = null; 
} 
相關問題