【问题标题】:Android starting an intent doesn't set the actionAndroid 启动意图不会设置操作
【发布时间】:2012-07-30 09:02:46
【问题描述】:

我正在处理推送通知。当前正在接收和显示通知,但点击事件未按应有的方式运行。

int NOTIFICATION_ID = 1593;

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.notification, title, System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        Intent notificationIntent =  new Intent(this, MyMainActivity.class);
        notificationIntent.putExtra("test", "test");
        notificationIntent.setAction("load_notifications");

        PendingIntent intentNotification = PendingIntent.getActivity(this, 999, notificationIntent, 0);
        notification.setLatestEventInfo(this, title,  body, intentNotification);
        notificationManager.notify(NOTIFICATION_ID, notification);

这就是我显示通知的方式,它还包括开始/恢复我的主要活动。

但是,如果我在应用程序中单击通知,putExtra("test", "test") 和 setAction 似乎无效。如果我的应用无法运行,从通知中启动它可以正常工作。

我的主要活动中的 onResume 函数:

public void onResume() {
        super.onResume();

        if(this.getIntent().getAction().equalsIgnoreCase("load_notifications")) {
//Do stuff
            }


    }

我的应用配置如下:

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Activity"
            android:label="@string/app_name" 
            android:configChanges="orientation"
            android:launchMode="singleInstance"
            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Push notifications -->

        <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.app.android" />
            </intent-filter>
        </receiver>

        <service android:name=".GCMIntentService" />

    </application>

【问题讨论】:

  • 我会尝试输入一些日志输出,以检查它是否根本没有进入那里,或者它是否只是没有进入你的 if 循环。也许将动作加载到变量并打印出来?
  • 我删除了代码块的日志调用。活动保持为主,而不是新设置的“load_notifications”,我添加的测试变量只是空。

标签: android android-activity android-notifications


【解决方案1】:

答案可能在这里:Action buttons are not shown in notification

API 16 中引入了通知操作。

这意味着您应该将目标 SDK 设置为 16 或更高版本。

但是,只有 Jelly Bean以上 可以利用通知操作 - 请务必在这些平台上测试此功能。

NotificationCompat.Builder 负责生成与运行它的任何 API 兼容的通知,因此如果您要支持 Ice Cream Sandwich 和更早版本,请继续使用它>。如果没有,只需使用 Notification.Builder 就足够了(当然是在更改目标 SDK 之后)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多