Android FCM推送通知,如何处理后台事件

标签 android push-notification

请高人帮忙解决。

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

这是main activity中写的代码,如果app在前台就去else if部分,如果app在后台,连onBroadcastReceiver方法都没有进入,那么后台事件怎么处理?

最佳答案

可以使用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" />

关于Android FCM推送通知,如何处理后台事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45229249/

相关文章:

php - Android中C2DM的一些查询

android - 推送通知状态检查 Android

iOS:为推送通知注册设备

java - 如何在按下按钮时显示这样的选项菜单?

android - 如何知道组件在其父组件中可以承担的大小?

java - 使用 DatagramPacket 发送 0x04 - 类似于十六进制 char[] 数据

android - 独立世博会(ExpoKit)推送通知

android - 停止 Android 操作系统自动更新

android.content.res.Resources$NotFoundException : String resource ID #0x1 Error

android - 通知中的未决 Intent 未被清除