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

时间:2017-07-21 04:55:33

标签: 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);
            }
        }
    };

这是在主活动中编写的代码,如果应用程序在前台,则转到其他部分,如果应用程序在后台,它甚至不进入onBroadcastReceiver方法,那么我该如何处理后台事件?

4 个答案:

答案 0 :(得分: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" />

答案 1 :(得分: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) {
    }
}

答案 2 :(得分: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

答案 3 :(得分:0)

能够处理推送通知前景和后台事件,在Service类和mainactivity中创建了一个通知方法,添加了以下代码

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