android - API < 11 上的 NotificationCompat.Builder 不工作

标签 android notifications android-2.3-gingerbread

public static final String NOTIFICATION_TITLE = "Title";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(NOTIFICATION_TITLE)
            .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(msg))
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setContentText(msg);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

上面的代码在我的 Nexus 4 (Android 4.3) 上运行顺利,但我的旧 Nexus One (Android 2.3.7) 仍然向我显示通知。

list 文件

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="my.package.matchtracker.permission.C2D_MESSAGE" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<permission
    android:name="my.package.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<application
    android:name=".AppClass"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="my.package" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="my.package" />
        </intent-filter>
    </receiver>
    <service android:name=".GcmIntentService" />

    <activity
        android:name="my.package.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我的场景

  • Ubuntu下的Android Studio 0.2.8
  • Android SDK 工具 22.2.1
  • Android SDK 平台工具 18.0.1
  • Android SDK 构建工具 17
  • Android 支持库 2
  • Android 支持库 18
  • Google Play 服务 12

我的 build-gradle我的项目中的文件

dependencies {
    compile 'com.android.support:support-v4:18.0.+', 'com.google.android.gms:play-services:3.1.+'
}

到目前为止我做了什么

  • 我知道 Gingerbread 需要 PendingIntent这就是为什么 .setContentIntent已设置
  • 我还尝试创建一个在 if(Build.VERSION.SDK_INT < 11) 上调用的函数
  • 即使是旧的(但已弃用)Notification也不行
  • 服务器按预期工作,因为 4.3 设备完美地接收了通知

所以,现在我已经没有新想法了,任何人都可以告诉我为什么通知在 API 10 上不起作用的任何其他选择?

最佳答案

我和你有同样的问题,显然我只注意到只有 Gingerbread 版本的效果

这是对我有用的修复 Android : Notification not working on 2.3.6 (Samsung galaxy y)

关于android - API < 11 上的 NotificationCompat.Builder 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18931424/

相关文章:

网络事件上/下/关的 iOS 事件/通知

java - 最佳实践 : Input Validation (Android)

android - 如何在 android 中获取雅虎联系人

android - 如何在尊重用户声音设置的情况下播放铃声?

android - 选项卡的自定义 Holo 主题

android - Android 中的 EditText 小部件

java - Android NumberPicker 对 Gingerbread 的限制

android - 按钮大小相对于 XML 布局中的父大小?

android - Qt TouchBegin touchPoint.pos() 停留在 0,0

通知服务中的android内存泄漏