0

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?

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64

1 Answers1

0

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());
    }
Vadim Eksler
  • 865
  • 9
  • 24
  • unfortunately same result. I m sending 1st notification, and 1 notification arrives, and I don't touch or cancel it, and sending 2nd notification, but I got 2 notification this time 1st and 2nd. So I have 3 notifications in total. – madProgrammer Aug 09 '18 at 08:58
  • it just calls sendNotification() method, and I shared codes of this method at above. – madProgrammer Aug 09 '18 at 09:09
  • you can look this [link](https://stackoverflow.com/questions/12808959/canonical-registration-id-and-message-id-format/12978274#12978274) – Vadim Eksler Aug 09 '18 at 10:17
  • and this [link](https://stackoverflow.com/questions/38167125/firebase-send-push-notification-twice) – Vadim Eksler Aug 09 '18 at 10:18
  • it looks like your problem – Vadim Eksler Aug 09 '18 at 10:19
  • but also check if you used `PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);` from my example, it very important – Vadim Eksler Aug 09 '18 at 10:19
  • isnt pendingIntent run if I click on notification popup ? I have no problems if I click on notifications. If I don't click on them, they pop again with next notification. – madProgrammer Aug 09 '18 at 10:25
  • also you can try `PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);` – Vadim Eksler Aug 09 '18 at 10:30
  • none of them worked... Weird. I'm using same code with firebase tutorial... But still not working properly. – madProgrammer Aug 09 '18 at 10:54
  • and you do this `mBuilder.setContentIntent(pendingIntent);` – Vadim Eksler Aug 09 '18 at 11:05
  • before you try my code, try to force app in settings and clear data and cache, and remove app from settings – Vadim Eksler Aug 09 '18 at 11:08
  • I've already done that. onMessageReceived() calls multiple times in Oreo. Problem is just in Oreo... nougat,marshmellow works correctly. – madProgrammer Aug 09 '18 at 11:18