簡體   English   中英

Android通知頻道使用新通知推送較舊的未讀通知

[英]Android Notification Channel Pushes Older Unread Notifications With New Notifications

我是Android通知頻道的新手。 我遵循了Google Dev文檔中的指南,但是在推送通知時遇到了一些麻煩。 我的問題是,如果我收到通知並輕掃取消或只是不單擊它,則以下通知與較舊的輕掃已取消或未更改的通知一起提供。 通知正在累積增加。

我的代碼如下:

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);

模塊搖籃

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'

我在哪里犯這個錯誤?

試試這個代碼

        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());
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM