[更新] 图标问题已修复,但仍需要通知来振动设备。
AndroidManifest权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
我正在学习android开发,现在尝试通过stackoverflow的答案使用此代码设置推送通知
String CHANNEL_ID = "my_channel_01";
public void addNotification(View view){
int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_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});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.myappnotificon)
.setContentTitle("Title")
.setContentText("Notification body");
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
在虚拟设备上进行测试看起来不错,但是在Android 6和7真实设备上进行测试后,该图标已替换为绿色图标(如随附的图片),并且不会振动。
那么如何解决图标问题并使设备振动?
答案 0 :(得分:1)
您需要更改android:roundIcon
文件中的 AndroidManifest.xml
属性。
在AndroidManifest.xml
文件中设置了默认圆形图标
android:roundIcon="@mipmap/ic_launcher_round"
您只需要用图标替换它即可。 即:
android:roundIcon="@mipmap/your_logo"
如下图所示,您需要将徽标设置为 android:icon=
以及 android:roundIcon=
注意:,然后我们可以在android:roundIcon=
文件中设置 AndroidManifest.xml
。
您可以从this page.
获得更多对于振动问题,此问题将得到解决
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setVibrate(new long[]{500, 500})
.setSmallIcon(R.drawable.my_app_icon)
.setContentTitle("Title")
.setContentText("Notification body");
并从 if
语句中删除以下行:
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});