简体   繁体   中英

Android Notification Channel Pushes Older Unread Notifications With New Notifications

I'm new with the Android Notification Channel. I followed the tutorials from Google Dev docs about it, but I'm having some trouble when pushing notifications. My problem is, if I get a notification and swipe-cancel it or just don't click on it, the following notifications come with older swipe-cancelled or untouched notifications. Notifications are increasing cumulatively.

My code is below:

NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    String channelId = "some_channel_id";
    CharSequence channelName = "Some Channel";
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.enableVibration(true);
    notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    notificationManager.createNotificationChannel(notificationChannel);


    int num = (int) System.currentTimeMillis();

    Notification notification = new Notification.Builder(this)
            .setContentTitle("Some Message")
            .setContentText("You've received new messages!")
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.fav_ico)
            .setChannelId(channelId)
            .build();

    notificationManager.notify(num, notification);

Module Gradle :

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
    compile "com.android.support:appcompat-v7:26.0.+"
    compile 'com.google.firebase:firebase-core:12.0.1'
    compile 'com.google.firebase:firebase-messaging:12.0.1'
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'id.zelory:compressor:2.1.0'
    compile 'net.gotev:uploadservice:3.0'
    compile 'dev.dworks.libs:volleyplus:+'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

apply plugin: 'com.google.gms.google-services'

Where am I making this mistake?

try this code

        int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel mChannel = new NotificationChannel(LOCATION_CHANNEL, LOCATION_NAME, importance);
    mChannel.enableLights(false);
    mChannel.enableVibration(false);

    int id = getApplicationInfo().icon;

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, mChannel.getId())
            .setSmallIcon(id)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.location_notification_text))
            .setPriority(NotificationManager.IMPORTANCE_DEFAULT)
            .setVibrate(new long[] {0L})
            .setChannelId(mChannel.getId())
            .setAutoCancel(true);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (manager != null) {
        manager.createNotificationChannel(mChannel);
        manager.notify(NOTIFICATION_ID, mBuilder.build());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM