我有一个非常简单的Service
触发通知。但它不起作用。
正如您可能看到的,所有这些都只是从网上复制过来,但我希望它在我调整之前能够正常工作。
这是从服务中调用的:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "my_channel_01";
CharSequence name = "someName";
String description = "even bigger name";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.big_anchor)
.setContentTitle("My notification")
.setChannel(id)
.setContentText("Hello World!");
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(getApplicationContext(),
0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager.notify(32345, mBuilder.build());
我已经读过,有些参数应该放在AndroidManifest.xml
中,所以我也附上了这个:
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GpsService" />
</application>
此外,我收到的唯一日志是:
10-30 22:44:13.939 417-417/asdf.appW/Notification: Use of stream types is deprecated for operations other than volume control
10-30 22:44:13.940 417-417/asdf.app W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
有什么想法吗?
更新
我想这与我正在使用的API版本有关。当我使用API 24时,一切正常。但是当使用带有26的仿真器时,什么也没有出现。
答案 0 :(得分:2)
答案 1 :(得分:-1)
我也遇到过类似的问题,NotificationCompat.Builder对我来说不适用于API 26.尝试将Notification.Builder用于API 26,将NotificationCompat.Builder用于低于26的版本。