android


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:
Logged all parts of code. Every block is executed flawlessly.
I have a feeling it has something to do with the context since the notification is fired from within the dialog's context. I am using a custom dialog. I tried changing to "ApplicationContext" but still doesn't work.
tried giving fixed values for notification Id and requestCode. No luck.
Thanks.
You need to set
.setSmallIcon(R.drawable.new_mail)
without setting the icon, notification won't show up.

Related Links

How can i send my object to server once the connection is established
how to delete all contacts in contact list on android mobile programatically
How to speed up the scrolling speed of GridView when the photos are all in local
Android ContactsContract.Contacts slow
Window Already Focused
String array list display problem
Android SDK: how to read a USB device's class?
Activity's launch modes
Populating android spinner with dynamic array name
android how to put a dialog box after pressing a back button
Implement game idea into andengine(Rango stretchable tongue)
get text from a child view in custom listadapter
Point to files inside the apk?
How to scroll ListView to a particular id and set that item as checked?
Store data into remote database via web service in Android
How to get bluetooth setting values

Categories

HOME
go
google-cloud-platform
jpa
hyperlink
interface
pascal
switch-statement
permissions
missing-data
performancepoint
pcl-crypto
minecraft-forge
activecollab
renjin
plink
android-intent
dynamic-sql
google-data-studio
html5-appcache
guzzle
href
systemtime
hta
php-mysqlidb
onload
moses
datastax-enterprise-graph
gp
ntp
scatter3d
joomla2.5
alphabetical
urlsession
install.packages
xbmc
proget
openblas
podscms
arq
amq
stdmap
skin
dt
monkey
gpg-signature
flyout
gameplay-kit
custom-lists
xcode-server
flex4.6
neon
macaulay2
x-tag
i386
hclust
android-dateutils
tracker-enabled-dbcontext
fink
nio2
invoke-sqlcmd
computer-architecture
uncompress
zxspectrum
execve
packet-sniffers
primitive-types
webfinger
encryption-asymmetric
ss7
malformedurlexception
getopt
syndicationfeed
solace-mq
model-driven-development
mvc-editor-templates
poker
scmmanager
jquery-blockui
os.system
subgraph
oracle-enterprise-linux
httponly
cross-database
google-maps-mobile
user-tracking
jquery-tools
filemerge
xcelsius
clrstoredprocedure
event-receiver
presentation-layer
eclipse-marketplace
product-management

Resources

Encrypt Message