TheWiz
TheWiz

Reputation: 115

Android Notification does not appear but sound is played

I have an app with a single activity. There's a FAB and when I press on it, it opens a dialog. When I add text, this is used as notification title and content and fires a notification when a button is pressed in the dialog. The issue is that the notification isn't displayed but the sound is played and I can't figure out why.

Notification Code:

public class NotificationUtils {

    public static void showNewNoteNotification(Context context, Class T, String title, String content, boolean isPersistent) {

        int notificationId = getNotificationId();
        Log.e("notificationId", String.valueOf(notificationId));

        Intent intent = new Intent(context, T);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setAction(Intent.ACTION_MAIN);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, getRequestCode(),
                intent, PendingIntent.FLAG_CANCEL_CURRENT);

        showNotification(context, pendingIntent, title, content, notificationId, true, isPersistent);
    }

    private static void showNotification(Context context, PendingIntent pendingIntent, String title,
                                         String content, int notificationId, boolean playSound, boolean isPersistent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.setContentIntent(pendingIntent)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(false)
                .setContentTitle(title)
                .setContentText(content)
                .setOngoing(isPersistent)
                .addAction(R.drawable.navigation_accept_dark, "READ", getReadPendingIntent(context, notificationId));

        if (playSound) {
            builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        }

        Notification notification = builder.build();

        notificationManager.notify(notificationId, notification);
        Log.e("Notification generated","yes");
    }

    private static int getNotificationId() {
        Random random = new Random();
        return random.nextInt(9999 - 1000) + 1000;
    }

    private static int getRequestCode() {
        Random random = new Random();
        return random.nextInt(99999 - 10000) + 10000;
    }

    private static PendingIntent getReadPendingIntent(Context context, int notificationId) {
        Intent intent = new Intent("com.theappnazi.notificationnotes.intent.NOTE_MARKED_READ");
        intent.putExtra(AppConstants.NOTIFICATION_ID, notificationId);
        return PendingIntent.getBroadcast(context, getRequestCode(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
        //TODO: CHECK IF FLAG_CANCEL_CURRENT DOESNT CANCEL ANY OTHER NOTIFICATIONS
    }
}

Dialog Code (MessageUtils.java):

public static void showAddNoteDialog(final Context context, final NoteDataSource noteDataSource, final AlertDialogCallback alertDialogCallback) {
    if (context != null && context.getResources() != null) {
        final Dialog dialog = new Dialog(context);
        dialog.setTitle(R.string.add_note_dialog_title);
        dialog.setContentView(R.layout.layout_add_note_dialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();

        Button button = (Button) dialog.findViewById(R.id.add_button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText noteTitle = (EditText) dialog.findViewById(R.id.notification_title_edittext);
                EditText noteContent = (EditText) dialog.findViewById(R.id.notification_content_edittext);
                CheckBox persistentCheckBox = (CheckBox) dialog.findViewById(R.id.checkbox_persistent);

                String notificationTitle = noteTitle.getText().toString();
                String notificationContent = noteContent.getText().toString();

                String noteDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

                if (ValidationUtils.checkValidity(notificationTitle, AppConstants.DATA_TYPE_GENERAL_TEXT, context)) {
                    Log.e("Validation passed","yes");
                    NotificationUtils.showNewNoteNotification(context, MainActivity.class, notificationTitle, notificationContent, persistentCheckBox.isChecked());
                    noteDataSource.createNote(notificationTitle, notificationContent, noteDate);
                }

                alertDialogCallback.onButtonClick(dialog, 0, AppConstants.ADD_BUTTON_CLICKED);
            }
        });


    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {

private NoteDataSource noteDataSource;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    noteDataSource = new NoteDataSource(this);
    noteDataSource.open();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAddNoteDialog(noteDataSource);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void showAddNoteDialog(NoteDataSource noteDataSource) {
    MessageUtils.showAddNoteDialog(MainActivity.this, noteDataSource, new MessageUtils.AlertDialogCallback() {

        @Override
        public void onButtonClick(DialogInterface dialogInterface, int id, String clickedButtonType) {
            if (dialogInterface != null)
                dialogInterface.dismiss();
        }
    });
}

@Override
protected void onPause() {
    noteDataSource.close();
    super.onPause();
}

@Override
protected void onResume() {
    noteDataSource.open();
    super.onResume();
}

}

Things I tried:

Thanks.

Upvotes: 0

Views: 78

Answers (1)

Nishchay Zacariah
Nishchay Zacariah

Reputation: 91

You need to set

.setSmallIcon(R.drawable.new_mail)

without setting the icon, notification won't show up.

Upvotes: 1

Related Questions