0

I am trying to create a notification after 6 seconds a button is pressed but the app doesn't show any notifications at all. Can anyone please have a look and try to figure out what I am doing wrong.

 @Override
            public void run() {

                int id = 1;
                Intent notificationIntent = new Intent(ConfirmOrderActivity.this,MainActivity.class);
                PendingIntent pIntent = PendingIntent.getActivity(ConfirmOrderActivity.this,id, notificationIntent,0);

                NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(ConfirmOrderActivity.this, DEFAULT_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_restaurant_menu_black)
                .setContentTitle("You Have a notification!")
                .setContentText("See Your Notification")
                .setContentIntent(pIntent);

                Notification notification = nBuilder.build();

                NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

                manager.notify(id, notification);
            }
        }, 6000);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Did you create the `NotificationChannel` for `DEFAULT_CHANNEL_ID` somewhere first? – Mike M. Sep 15 '19 at 07:52
  • No I didn't I just found that over here https://developer.android.com/reference/android/app/NotificationChannel – user12069329 Sep 15 '19 at 07:56
  • You need to create the `NotificationChannel` first, and you should define your own ID string for it. https://stackoverflow.com/a/44524976 – Mike M. Sep 15 '19 at 08:00
  • Thanks, but do I need to create this Notification Channel in the same method or I need to create a new method – user12069329 Sep 15 '19 at 08:10
  • It doesn't matter, technically. You just have to create the channel _before_ you post the `Notification`. And you only need to create it once. – Mike M. Sep 15 '19 at 08:12

2 Answers2

0

NotificationCompat.Builder have a property(.setWhen(time)). See below sample code:

NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_ansar_logo)
                .setContentTitle(pushModel.getTitle())
                .setContentText(pushModel.getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setAutoCancel(true)
                .setSound(notificationSoundURI)
                .setPriority(Notification.PRIORITY_MAX)
                .setWhen(6000)
                .setContentIntent(resultIntent);
-1

Check below code

 import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.RecyclerView;

import android.view.View;

import android.widget.Button;


import static android.app.NotificationChannel.DEFAULT_CHANNEL_ID;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                generateNotification();
            }
        });

    }

    void generateNotification() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do something after 100ms
                int id = 1;
                Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
                PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), id, notificationIntent, 0);

                NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("You Have a notification!")
                        .setContentText("See Your Notification")
                        .setContentIntent(pIntent);

                Notification notification = nBuilder.build();

                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                manager.notify(id, notification);
            }
        }, 6000);
    }
}
Emad Seliem
  • 608
  • 1
  • 4
  • 5