0

I've been searching around and tried different solutions but I can't seem to get this to work.

I have a MainActivity with a list and an "Add" button. When i click the addbutton i open a DialogFragment. In the dialog user inputs data and press "add" and i want a notification that says "xxxx has been added to list" but it never shows.

I have a seperate class for AddDialog which looks like this:

public class AddDialogFragment extends DialogFragment {
Button btnAdd;
Button btnCancel;
TextView etNewSerial, etNewBrand, etNewModel;
String newSerial, newBrand, newModel;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.dialog_add, container, false);
    getDialog().setTitle("Add new serial!");
    btnAdd = (Button) rootView.findViewById(R.id.btnAddNew);
    etNewSerial = (TextView) rootView.findViewById(R.id.etNewSerial);
    etNewBrand = (TextView) rootView.findViewById(R.id.etNewBrand);
    etNewModel = (TextView) rootView.findViewById(R.id.etNewModel);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             newSerial = etNewSerial.getText().toString();
             newBrand = etNewBrand.getText().toString();
             newModel = etNewModel.getText().toString();

            String url = "MY_VOLLEY_URL"
            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    int icon = 0;
                    CharSequence notiText = "Item Added!";
                    long notiTime = System.currentTimeMillis();
                    CharSequence notiTitle = "Item Added!";//Titel
                    CharSequence notiContent = newSerial + " added!";
                    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
                    NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(getActivity());
                    notBuilder.setSmallIcon(icon)
                            .setTicker(notiText)
                            .setWhen(notiTime)
                            .setAutoCancel(true)
                            .setContentText(notiContent)
                            .setContentTitle(notiTitle)
                            .setContentIntent(pendingIntent);
                    Notification notification = notBuilder.build();
                    NotificationManager myNotificationManager = (NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
                    myNotificationManager.notify(1, notification);
                    getDialog().dismiss();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            Volley.newRequestQueue(getActivity()).add(request);
        }
    });
    return rootView;
}
}

But the notification never shows. I've been trying to replace the "getActivity()" with different methods for context but it doesn't seem to matter. Does anyone has some tips to make this possible?

uckizz
  • 15
  • 5
  • What kind of Notification do you really want? Just a little popup that says "added to the list" or a real `Notification` that appears like new chat messages like new WhatsApp or Telegram messages? If you only need a little hint for the user, then check out a `Toast` or a `SnackBar` – deHaar Oct 06 '17 at 12:27
  • Are you getting the success response from API ? – Burhanuddin Rashid Oct 06 '17 at 12:32
  • Yes that works! – uckizz Oct 06 '17 at 12:34
  • Yeah i know, used toast before but for this assignment a notification was required! – uckizz Oct 06 '17 at 12:37
  • Could you please try to set a valid resource for smallIcon to test? Change to .setSmallIcon(android.R.color.transparent) – guipivoto Oct 06 '17 at 12:56
  • @GuilhermeP oh man, that did it! Downloaded an icon and put in the drawable folder and replaced "int icon = 0" with "int icon = R.drawable.notIcon" and sure enough, the notification showed. I though you could type "0" for testing purposes – uckizz Oct 06 '17 at 13:00
  • Good to know.. I'll post as answer then for future references... – guipivoto Oct 06 '17 at 13:02

1 Answers1

0

I wonder you are getting this error because your are passing 0 as the resourceId for the small icon (setSmallIcon). Maybe, NotificationManager is ignoring this notification since it can not built properly (there's no resource with 0 as ID.

So, try to change this:

notBuilder.setSmallIcon(icon)
    ....

To:

notBuilder.setSmallIcon(android.R.color.transparent)
    ....

I use android.R.color.transparent as example since you are not interested in show any icon anyway. But you can change to any color/drawable etc...

guipivoto
  • 18,327
  • 9
  • 60
  • 75