【问题标题】:NotificationCompat.Builder on API < 11 not workingAPI < 11 上的 NotificationCompat.Builder 不起作用
【发布时间】:2013-09-26 16:57:50
【问题描述】:
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) 仍然需要向我显示通知。

清单文件

<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 &lt; 11) 上调用函数
  • 即使是旧的(但已弃用)Notification 也不起作用
  • 服务器按预期工作,因为 4.3 设备完美接收通知

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

【问题讨论】:

  • 你试过android.support.v4.app.NotificationCompat.Builder
  • @MuhammadBabar 谢谢,但我已经为NotificationCompat.Builder 使用了支持 v4 库,这就是为什么我无法理解我的问题

标签: android notifications android-2.3-gingerbread


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多