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>
我的项目中的build-gradle
文件
dependencies {
compile 'com.android.support:support-v4:18.0.+', 'com.google.android.gms:play-services:3.1.+'
}
PendingIntent
这就是设置.setContentIntent
的原因if(Build.VERSION.SDK_INT < 11)
Notification
也不起作用所以,现在我已经用尽了新想法,任何人都可以告诉我为什么通知不能在API 10上运行的任何其他选项?
答案 0 :(得分:1)
我和你有同样的问题,很明显我只注意到效果只有Gingerbread版本
这是一个适合我的修复程序 Android : Notification not working on 2.3.6 (Samsung galaxy y)