其他分享
首页 > 其他分享> > android – 将谷歌云消息接收器中的意图附加内容传递给活动为空

android – 将谷歌云消息接收器中的意图附加内容传递给活动为空

作者:互联网

我有一个收听云消息的广播接收器,我可以很好地显示通知,但是当用户点击通知时我想用适当的附加功能将它们路由到正确的活动.额外的东西总是空的.

我可以验证传递给挂起的intent的意图在调试时是否有额外的功能.

我尝试了解活动onCreate()方法中的意图

***编辑添加工作代码,有几点需要注意,如果任务已经启动,你不能指望调用oncreate()方法,因此似乎最好的地方是在onresume方法中获取额外的东西.我需要在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());

}

}

活动

 @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-broadcast,android,google-cloud-messaging,android-intent
来源: https://codeday.me/bug/20190831/1774234.html