2017-07-21 95 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" />