来自firebase的e推送通知时打开特定活动

时间:2017-06-14 12:10:17

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

我有Android应用程序并尝试接收推送通知。 单击保留通知时我的问题打开主要活动, 我需要从firebase收到推送通知时直接打开特定活动。这是我的代码

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_a)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getNotification().getBody());
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e("Mo", "Exception: " + e.getMessage());
        }
        Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
        startActivity(resultIntent);
    }

    private void handleDataMessage(JSONObject json) {

        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();

            Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
            startActivity(resultIntent);
        } else {
            Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
            startActivity(resultIntent);
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以在通知中设置待处理意图。所以首先创建一个待定的意图:

NotificationBuilder

然后添加到您的builder.setContentIntent(contentIntent) ,您可以在其中定义通知的标题,说明等,以下参数:

NotificationCompatBuilder

<强>更新

您的代码中已有NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.app_logo_a) .setContentTitle(remoteMessage.getData().get("title")) .setContentText(remoteMessage.getNotification().getBody());

替换它:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_a)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getNotification().getBody())
                .setContentIntent(contentIntent);

用这个:

$sslOptionKeys = array(PDO::MYSQL_ATTR_SSL_KEY, PDO::MYSQL_ATTR_SSL_CERT, PDO::MYSQL_ATTR_SSL_CA);

foreach($sslOptionKeys as $sslOptionKey) {
   if(array_key_exists($sslOptionKey, $this->pendingAttributes)) {
       $pdoOptions[$sslOptionKey] = $this->getAttribute($sslOptionKey);
   }
}

$this->dbh = new PDO($this->options['dsn'], $this->options['username'],
                     (!$this->options['password'] ? '':$this->options['password']),
                     $pdoOptions);

答案 1 :(得分:0)

在点击通知时,您应该考虑使用PendingIntent来启动特定活动。将其添加到您的代码中

    Intent intent = new Intent(this, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, YOUT_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

您可以将代码更改为此,即使您未对handleDataMessage进行了详细说明,但这样您也可以在点击通知时启动活动

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));

    //two lines addded
    Intent intent = new Intent(this, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.app_logo_a)
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentIntent(contentIntent);//added line
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
    try {
        JSONObject json = new JSONObject(remoteMessage.getData().toString());
        handleDataMessage(json);
    } catch (Exception e) {
        Log.e("Mo", "Exception: " + e.getMessage());
    }

}

private void handleDataMessage(JSONObject json) {

    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
        // app is in foreground, broadcast the push message
        Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
        pushNotification.putExtra("message", message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
        NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
        notificationUtils.playNotificationSound();

    }
}