我有一个只有一项活动的应用。有一个FAB,当我按下它时,它会打开一个对话框。当我添加文本时,它将用作通知标题和内容,并在对话框中按下按钮时触发通知。问题是没有显示通知但声音播放,我无法弄清楚原因。
通知代码:
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
}
}
对话框代码(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();
}
}
我尝试的事情:
记录了代码的所有部分。每个街区都完美无缺地执行。
我觉得它与上下文有关,因为通知是从对话框的上下文中触发的。我正在使用自定义对话框。我尝试更改为“ApplicationContext”但仍然无效。
尝试为通知ID和requestCode提供固定值。没有运气。
感谢。
答案 0 :(得分:1)
您需要设置
.setSmallIcon(R.drawable.new_mail)
没有设置图标,通知就不会显示。