在Android中如何在扩展FirebaseMessagingService调用onMessageReceived时获取当前的Activity上下文?

Chi*_*g.T 8 service android interface android-context firebase-cloud-messaging

我的代码用于集成FCM ..我希望在推送到达时获取当前活动上下文.用于使用上下文转换侦听器的目的.

这里的代码片段代码......

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());
}
Run Code Online (Sandbox Code Playgroud)

}

创建界面

public interface NotificationListener {

public void onNotificationMessage(AlertList alertList, int i);
Run Code Online (Sandbox Code Playgroud)

}

呼唤另一个班级

public class Header extends AppCompatActivity implements NotificationListener{

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

@Override
public void onNotificationMessage(final int count) {

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

}
Run Code Online (Sandbox Code Playgroud)

}

我希望在没有来自另一个类的任何上下文引用的情况下获得当前活动上下文.

Ewo*_*oks 7

如果我理解正确,基本上你想将活动的上下文转换为活动实现的接口.问题是:您无法获取当前活动上下文,因为可能/可能根本没有当前活动.有一些上下文可用,但此上下文不能用于您想要的内容.让我来解释一下:

假设用户没有使用应用程序一周,他完成了所有活动,退出应用程序(或操作系统释放内存并清理所有内容).服务仍在后台运行,当推送消息到来时,它会根据您的代码做出反应(假设您立即想要显示通知).如果此时需要上下文,例如通知图标颜色,则可以使用以下上下文:

int color = ContextCompat.getColor(getApplicationContext(), R.color.colorAccent);
Run Code Online (Sandbox Code Playgroud)

getApplicationContext()将为您提供所需的上下文,但这当然不是活动上下文,因为仍然没有活动(所有这些都在几天前完成)

希望这个解释有助于理解这里的概念.为了更好地理解存在什么样的上下文,请检查上下文,什么上下文?