2017-05-04 1 views
0

Ich versuche, AlertDialog in OnMessageReceived zu verwenden, aber ich habe diesen Fehler.Ich kann AlertDialog nicht in OnMessageReceived verwenden

05-04 11:27:40.038 30721-31424/com.xxx.xxx E/AndroidRuntime: FATAL EXCEPTION: pool-4-thread-1 
                       Process: com.xxx.xxx, PID: 30721 
                       java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
                        at android.os.Handler.<init>(Handler.java:200) 
                        at android.os.Handler.<init>(Handler.java:114) 
                        at android.app.Dialog.<init>(Dialog.java:108) 
                        at android.app.AlertDialog.<init>(AlertDialog.java:125) 
                        at android.app.AlertDialog$Builder.create(AlertDialog.java:967) 
                        at android.app.AlertDialog$Builder.show(AlertDialog.java:986) 
                        at com.xxx.xxx.util.FirebaseMessagingService.optionalAlert(FirebaseMessagingService.java:78) 
                        at com.xxx.xxx.util.FirebaseMessagingService.onMessageReceived(FirebaseMessagingService.java:38) 
                        at com.google.firebase.messaging.FirebaseMessagingService.zzl(Unknown Source) 
                        at com.google.firebase.messaging.FirebaseMessagingService.zzJ(Unknown Source) 
                        at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source) 
                        at com.google.firebase.iid.zzb$1.run(Unknown Source) 
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                        at java.lang.Thread.run(Thread.java:818) 

Dies ist mein Code:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     super.onMessageReceived(remoteMessage); 
     String msg=remoteMessage.getNotification().getBody(); 
     Map<String, String> params = remoteMessage.getData(); 
     String Valuekey = remoteMessage.getData().get("key"); 

     if (Valuekey.equals("true")) { 
      optionalAlert(); 
     }else { 
      Util.Alert(this,getString(R.string.ConsignmentNoFound)); 
     } 

     // Util.Alert(getBaseContext(),msg); 
    } 

    private void optionalAlert() { 


     AlertDialog.Builder adb = new AlertDialog.Builder(this); 

     TextView textView = new TextView(this); 
     textView.setText(getString(R.string.ConsignmentFound)); 
     textView.setTextSize(24); 
     adb.setCustomTitle(textView); 


     //adb.setTitle(getString(R.string.ConsignmentFound)); 


     adb.setIcon(android.R.drawable.ic_dialog_alert); 


     adb.setPositiveButton("Si", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

       Intent nextActivity = new Intent(FirebaseMessagingService.this, MainActivity.class); 
       nextActivity.putExtra("act", "PinValidate"); 
       startActivity(nextActivity); 

      } }); 


     adb.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 


      } }); 
     adb.show(); 
    } 

} 

Antwort

0

Sie nicht Dialog zeigen in Receiver, da verschiedenen ‚Kontext‘ Erhalten hat, die nicht für das Darstellen jeden User Interface ist.

Wenn Sie Dialog anzeigen möchten, starten Sie eine Aktivität, die Dialog Theme haben wird.

+1

Obwohl dies immer noch zutrifft, ist FirebaseMessagingService ein Dienst. – Submersed

0

Sie haben Operationen auszuführen, die UI auf einen bestimmten Thread beeinflussen. In Anbetracht der Aktivitätsinstanz, Sie Zugriff auf eine praktische Methode, um genau das zu tun:

activity.runOnUiThread(new Runnable() { 
    public void run() { 
     // perform ui operations... 
    } 
}); 
+0

, wo ich dies umsetzen kann? in der Klasse FirebaseMessagingService auf in der tatsächlichen Aktivität? –

+0

Ruft eine Instanz der aktuellen laufenden Aktivität ab. – kailoon

0

In Ihrer MyFirebaseMessagingService Klasse: -

public void onMessageReceived(RemoteMessage remoteMessage) 
{ 

     context = this; 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message getMessageId: " + remoteMessage.getMessageId()); 
     Log.d(TAG, "Notification Message getMessage: " + remoteMessage.getData().get("message")); 
     Log.d(TAG, "Notification Message getAction: " + remoteMessage.getData().get("action")); 
     Log.d(TAG, "Notification Message template_id: " + remoteMessage.getData().get("template_id")); 
     Log.d(TAG, "Notification Message notification_user_id: " + remoteMessage.getData().get("notification_user_id")); 
     Log.d(TAG, "Notification Message joined_user_id: " + remoteMessage.getData().get("sender_id")); 

     //Calling method to generate notification 
     sendNotification(remoteMessage.getData().get("message"), remoteMessage.getData().get("action"), 
       remoteMessage.getData().get("template_id"), 
       remoteMessage.getData().get("notification_user_id"), 
       remoteMessage.getData().get("sender_id")); 
    } 

private void sendNotification(final String messageBody, String messageAction, String templateId, String notiUserId, String sender_id) { 
      if (ApplicationClass.getInstance().getCurrentClass() != null) { 
       //Let this be the code in your n'th level thread from main UI thread 
       Handler h = new Handler(Looper.getMainLooper()); 
       h.post(new Runnable() { 
        public void run() { 
         Common.displayAlert(ApplicationClass.getInstance().getCurrentClass(), messageBody); 
        } 
       }); 
      } 

} 

Und In Common Klasse eine Methode macht einen Dialog anzuzeigen; -

public static void displayAlert(final Context context, String respMsg) { 
     try { 
      AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setCancelable(false); 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Intent splashIntent = new Intent(context, MobileNoVerificationActivity.class); 
        splashIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(splashIntent); 
       } 
      }); 
      builder.setTitle("" + context.getString(R.string.app_name)); 
      builder.setMessage(respMsg); 
      builder.create().show(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

Hoffnung, dies hilft Ihnen

Verwandte Themen