2017-07-21 3 views
1

Jemand bitte helfen, es zu lösen.Android FCM Push-Benachrichtigung, wie mit Hintergrundereignis zu behandeln

mRegistrationBroadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // checking for type intent filter 
      if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) { 
       // gcm successfully registered 
       // now subscribe to `global` topic to receive app wide notifications 
       FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL); 
       displayFirebaseRegId(); 
       System.out.println("If condition :" + Config.REGISTRATION_COMPLETE + "::" + Config.PUSH_NOTIFICATION); 

      } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) { 
       // new push notification is received 
       String message = intent.getStringExtra("message"); 

       showAlertDialog(MainActivity.this, "Alert", message, true); 
       txtMessage.setTextColor(Color.GREEN); 
       Picasso.with(context).load(message).into(iImageView); 
       // txtMessage.setText(message); 
       System.out.println("Else condition :" + Config.REGISTRATION_COMPLETE + "::" + Config.PUSH_NOTIFICATION); 
      } 
     } 
    }; 

Dies ist der Code in Haupttätigkeit geschrieben, wenn der App im Vordergrund geht es anders, wenn ein Teil, wenn die App im Hintergrund ist es nicht einmal in onBroadcastReceiver Methode geben wird, dann kann, wie Ich handle Hintergrundereignis?

+0

Was ha Hast du bisher erreicht? –

+0

bekam eine Demo, in der Lage, eine Nachricht von Firebase Web zu senden, und empfangen in der App, wollen seine Hintergrundereignis zu fangen, und wenn ich auf Benachrichtigung klicken, ich möchte die Nachricht in Aktivität – Developer

+0

PLZ Ihren Code für die Benachrichtigung .. ich denke Sie haben diesen Code in Service geschrieben –

Antwort

0

Kann manifest.xml behandeln Benachrichtigungen Vordergrund- und Hintergrundereignisse drücken, erstellt ein Verfahren zur Benachrichtigung Serviceklasse und in Hauptaktivität hinzugefügt der folgende Code

if (getIntent().getExtras() != null) { 
     System.out.println("Coming to if method"); 
     String sMessage = getIntent().getStringExtra("message"); 
     String sImageUrl = getIntent().getStringExtra("image"); 
     String sPhoto = getIntent().getStringExtra("photo"); 
     System.out.println("Result :" +sMessage + "::" + sImageUrl + "::" + getIntent().getStringExtra("is_background")); 
     for (String key : getIntent().getExtras().keySet()) { 
      String value = getIntent().getExtras().getString(key); 
      if (key.equals("is_background") && value.equalsIgnoreCase("True")) { 
       txtMessage.setText("Success :" + sMessage); 
       Picasso.with(this).load(sPhoto).into(imageView); 
      } 
     } 
    } 
0

können Sie verwenden

private void generateNotification(Context context, String message) { 
    int icon = R.mipmap.app_icon; 
    final int soundResId = R.raw.notification_sound; 
    try { 
     Intent intent = new Intent(this, TragetActivityName.class); 
     intent.putExtra("usedfor", ""); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.driver_app_ico) 
       .setContentTitle("Application name") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager1 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager1.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } catch (Exception e) { 
    } 
} 
0

So Inorder zu Benachrichtigungssymbol Sie dies in Ihrem Android-Manifest hinzufügen zu ändern.

<meta-data 
    android:name="com.google.firebase.messaging.default_notification_icon" 
    android:resource="@drawable/ic_stat_ic_notification" /> 

Ändern Sie die Symbolformularressourcen.

Dies ist die einfachste Methode zum Ändern des Benachrichtigungssymbols. Sie können durch Benachrichtigung Farbe ändern

<meta-data 
    android:name="com.google.firebase.messaging.default_notification_color" 
    android:resource="@color/colorAccent" /> 

hinzufügen und diese Antwort in Stack Overflow folgen bestimmte Aktivität auf Klick mitteilungs zu öffnen. Lesen fcm Dokumentation Here

+0

Vordergrundereignis ist gut, aber im Hintergrund, wenn Benachrichtigung kommt, onclick es Ich möchte zu neuen Aktivitäten mit den empfangenen Daten verschieben – Developer

1

Sie FCM Downstream-Service von

können
public class FCMMessageHandler extends FirebaseMessagingService { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map<String, String> data = remoteMessage.getData(); 
     String from = remoteMessage.getFrom(); 
     String title = data.get("title"); 
     String content = data.get("content"); 

     // here you need parse a message and .... 
    } 

    // Creates notification based on title and body received 
    private void createNotification(String title, String content, long id, Intent intent) { 
     Context context = getBaseContext(); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, 0); 
     android.support.v4.app.NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(context) 
         .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title) 
         .setContentIntent(pendingIntent) 
         .setAutoCancel(true) 
         .setContentText(content); 

     NotificationManager mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify((int) id, mBuilder.build()); 
    } 
} 

hinzufügen

<service 
     android:name=".firebase.FCMMessageHandler" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service> 

    <meta-data 
    android:name="com.google.firebase.messaging.default_notification_icon" 
     android:resource="@drawable/common_google_signin_btn_icon_dark" /> 

    <meta-data 
    android:name="com.google.firebase.messaging.default_notification_color" 
     android:resource="@color/colorAccent" />