2017-10-06 11 views
0

私は周りを探し回っていて、さまざまな解決策を試しましたが、これを動作させることはできません。DialogFragmentクラス内に通知を作成しても機能しません。 (Android)

私は、リストと「追加」ボタンでMainActivityを持っています。私が追加ボタンをクリックすると私はDialogFragmentを開きます。ダイアログのユーザーがデータを入力して「追加」を押すと、「xxxxがリストに追加されました」という通知が表示されますが、表示されません。

私はこのようになりますAddDialogのための別々のクラスがあります。

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; 
} 
} 

しかし、通知は表示されませんが。私は "getActivity()"をコンテキストのための異なるメソッドに置き換えようとしてきましたが、それは重要ではないようです。誰にもこれを可能にするヒントがありますか?

+0

どのような種類の通知あなたは本当に欲しいですか? 「リストに追加されました」という少しのポップアップか、新しいWhatsAppメッセージや電文メッセージなどの新しいチャットメッセージのように表示される実際の「通知」ですか? ユーザーに少しだけヒントが必要な場合は、「Toast」または「SnackBar」をチェックしてください – deHaar

+0

APIからの成功応答を受け取っていますか? –

+0

はい、動作します! – uckizz

答えて

0

0が小さなアイコン(setSmallIcon)のresourceIdとして渡されているため、このエラーが発生しているのでしょうか。たぶん、NotificationManagerは、それが適切に構築することができないので、この通知を無視している(IDとして0持つリソースがない

ので、この変更しよう:。

notBuilder.setSmallIcon(icon) 
    .... 

へ:

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

Iをとにかく任意のアイコンを表示することに関心がないので、android.R.color.transparentを使用してください。しかし、任意の色/描画可能なものに変更することができます。

関連する問題