【问题标题】:Android GCM putExtra on opening intentAndroid GCM putExtra 打开意图
【发布时间】:2014-05-08 13:48:54
【问题描述】:

我正在尝试使用 GCM 打开一个带有给定额外字符串的 Intent。我之所以要这样做是为了打开一个特定的片段并从某个对象加载数据。

现在,每当我收到通知并单击它时,我的应用就会打开并启动一个主 Activity。我唯一需要的是将扩展数据添加到打开意图(简单字符串)。

GcmIntentService 代码如下:

public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GcmIntentService() {
        super("GcmIntentService");
    }
    public static final String TAG = "GCM Demo";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM will be
             * extended in the future with new message types, just ignore any message types you're
             * not interested in, or that you don't recognize.
             */
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                Log.e("GCM", "Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                Log.e("GCM", "Deleted messages on server: " + extras.toString());
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification(extras);
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(Bundle extras) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        String message = extras.getString("message");
        Intent openIntent = new Intent(this, HomeActivity.class);
        openIntent.putExtra("hintid", extras.getString("hintid"));
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                openIntent, 0);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("StadseBoeren")
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(message))
        .setContentText(message);

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

这里是对应的主Activity代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_home);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            if (extras.containsKey("hintid")) {
                String hintid = extras.getString("hintid");
                Log.d("GCM hintid: ", hintid);
                // TODO: Do something with the value of isNew.
            }
        }

不知何故 extras 是空的

【问题讨论】:

    标签: android android-intent push-notification google-cloud-messaging android-pendingintent


    【解决方案1】:

    显然,PendingIntent.getActivity(...) 工厂方法只是重新使用旧意图作为优化,即使它设置为 0。为确保不会发生这种情况,请传递标志 PendingIntent。 FLAG_CANCEL_CURRENT 像这样:

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    openIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    

    【讨论】:

    • 以上解决了我的问题。上述更改后,您的 getExtra 仍然为空。请确保您在 Intent 和 PendingIntent 之间放置了额外的内容。我犯了这个错误,所以我和你分享一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多