gpt4 book ai didi

android - 将 intent extras 从谷歌云消息接收器传递到 Activity 是空的

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:52 25 4
gpt4 key购买 nike

我有一个监听云消息的广播接收器,我可以很好地显示通知,但是当用户单击通知时,我想将他们路由到带有 intent extras 的正确 Activity 。额外的总是空的。

我可以验证传递给挂起 Intent 的 Intent 在调试时确实有额外的东西。

我尝试了解 Activity onCreate() 方法中的 Intent

***编辑添加工作代码,有几点需要注意,如果任务已经开始,你不能指望调用 oncreate() 方法,所以看起来最好的地方是获得额外的东西onresume 方法。我需要在 Intent 和未决 Intent 上设置标志

@EReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "googlecloudmessage";
private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
private Context ctx;

@Pref
public MyPrefs_ myPrefs;

@Override
public void onReceive(Context context, Intent intent) {

Log.d(TAG, "received gcm message");

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
this.ctx = context;

String messageType = gcm.getMessageType(intent);

if (messageType != null) {

if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {

}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {

}
else if (messageType.equalsIgnoreCase("gcm")) {
myPrefs.notification().put(true);
handleNotification(intent);
}
}
setResultCode(Activity.RESULT_OK);
}

private void handleNotification(Intent intent) {

Bundle bundle = intent.getExtras();

String typeString = bundle.getString("type");
String icon = bundle.getString("icon");

String title = null;
String body = null;
Class<?> intentClass = null;
Integer intentExtraId = null;

if (typeString == null)
return;

int type = Integer.parseInt(typeString);

switch (type) {

case 1:

intentClass = FriendsActivity_.class;

title = bundle.getString("username");

break;

case 2:

intentClass = UserProfileActivity_.class;

title = bundle.getString("username");

intentExtraId = Integer.parseInt(bundle.getString("user_id"));

break;




}


mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);


Intent i = new Intent();
i.setClass(ctx, intentClass);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("gcm", true);
if (intentExtraId != null) {

i.putExtra("id", intentExtraId);
}

int requestID = (int) System.currentTimeMillis();

PendingIntent pendingIntent = PendingIntent.getActivity(ctx, requestID, i, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);

mBuilder.setSmallIcon(R.drawable.ic_stat_u);

Bitmap bmp = null;

try {
bmp = Ion.with(ctx, ctx.getResources().getString(R.string.image_container) + icon).asBitmap().get();
}
catch (InterruptedException e) {

e.printStackTrace();
}
catch (ExecutionException e) {

e.printStackTrace();
}

if (icon != null) {
mBuilder.setLargeIcon(bmp);
}

mBuilder.setContentTitle(title).setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

Activity

 @Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}

@Override
protected void onResume() {

super.onResume();

Integer newId = getIntent().getExtras().getInt("id");

id = newId;


}

最佳答案

这是您用来获取 PendingIntent 的调用:

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, i, 0);

如果系统中已经有一个匹配的PendingIntent,这个调用将只返回那个。它可能包含也可能不包含您想要的额外内容。为确保您的附加功能得到使用,您必须确保更新任何当前的 PendingIntent,如下所示:

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

关于android - 将 intent extras 从谷歌云消息接收器传递到 Activity 是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com