2017-07-21 103 views
1

有人請幫忙解決它。Android FCM推送通知,如何處理後臺事件

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

這是寫在主要活動的代碼,如果應用程序是在前景不言而喻否則,如果一部分,如果應用程序在後臺運行,它甚至不進入onBroadcastReceiver方法,那麼如何才能我處理背景事件?

+0

什麼公頃你到目前爲止取得了什麼? –

+0

得到了一個演示,能夠發送消息從firebase網站,並在應用程序接收,希望趕上其背景事件,當我點擊通知,我想提出消息在活動 – Developer

+0

PLZ張貼您的代碼通知..我認爲您將該代碼寫入服務 –

回答

0

能夠處理推送通知的前景和背景的事件,創造了通知的方法服務等級和主要活動添加了以下代碼

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

您可以使用

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

所以中序變更通知圖標在您的Android清單添加此。

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

更改圖標表單資源。

這是更改通知圖標最簡單的方法。 您可以通過添加

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

更改通知顏色,並按照這個答案在Stack Overflow上打開的通知點擊特定的活動。閱讀FCM文檔Here

+0

前臺事件工作正常,但在後臺通知時,點擊它,我想移動到收到數據的新活動 – Developer

1

可以使用FCM

的下游服務
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()); 
    } 
} 

添加到manifest.xml中

<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" />