当app在后台时修改android中的firebase推送通知

时间:2017-04-06 11:54:29

标签: android firebase push-notification firebase-cloud-messaging

我只是在应用程序处于后台时收到推送通知,我无法找到当我的应用程序接收推送时确切触发的内容。我只想更改通知正文,例如,如果通知消息是" hi"我想向用户" hi user&#34 ;.

public class MyFcmListenerService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage message) {
        //nothing triggered here when app is in background
    }

}

2 个答案:

答案 0 :(得分:1)

$fields = array(
'registration_ids' => $reg_id ,
'priority' => "high",
'data' => array(******));    
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

我遇到了同样的问题。从我的PHP firebase服务中删除了通知键值。我的问题得到了解决。我只是使用registration_idsprioritydata

答案 1 :(得分:0)

你可以,你只需要知道firebase推送通知在android中是如何工作的。

你需要覆盖

  

handleIntent函数。

此功能在后台处理Firebase通知。因此,在其内部,您将通过推送消息发送所有数据进行推送通知。别忘了从邮件中提取信息。您可以使用标题或正文等默认空格,也可以发送一些自定义数据。

接下来,我将附上示例代码的工作原理。

注意:如果您没有这种方法,那么您需要将firebase版本升级到10.0.1以上

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }

    @Override
    public void handleIntent(Intent intent) {
        //super.handleIntent(intent);

        Log.d(TAG,"Handling Intent");
        Bundle mBundle = intent.getExtras();
        String img = mBundle.getString("imgURL");
        String title = mBundle.getString("gcm.notification.title");
        String body = mBundle.getString("gcm.notification.body");
        mBundle.putInt("promoId",Integer.valueOf(mBundle.getString("promoId")));
        Integer id = mBundle.getInt("promoId");

        sendNotification(mBundle);
    }

    private void sendNotification(Bundle mBundle) {
        // Create an explicit content Intent that starts the main Activity.
        Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

        notificationIntent.putExtras(mBundle);

        // Construct a task stack.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        // Add the main Activity to the task stack as the parent.
        stackBuilder.addParentStack(MainActivity.class);

        // Push the content Intent onto the stack.
        stackBuilder.addNextIntent(notificationIntent);

        // Get a PendingIntent containing the entire back stack.
        PendingIntent notificationPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // Get a notification builder that's compatible with platform versions >= 4
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        String title = mBundle.getString("gcm.notification.title");
        String body = mBundle.getString("gcm.notification.body");

        // Define the notification settings.
        builder.setSmallIcon(R.mipmap.ic_launcher)
                // In a real app, you may want to use a library like Volley
                // to decode the Bitmap.
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                        R.mipmap.ic_launcher))
                .setColor(Color.RED)
                .setContentTitle(title)
                .setContentText(body)
                .setContentIntent(notificationPendingIntent);

        // Dismiss notification once the user touches it.
        builder.setAutoCancel(true);

        // Get an instance of the Notification manager
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Issue the notification
        mNotificationManager.notify(0, builder.build());
    }

}

如果您有任何疑问,我会编辑mi答案。