点击通知不会在后台打开活动

时间:2018-04-07 10:30:55

标签: android push-notification android-notifications firebase-notifications

我从firebase发送通知,而在前台点击通知时运行的应用程序将完美运行。但是如果应用程序在后台运行,那么点击通知就不会起作用。它没有打开 MainActivity 。我帮我错了。

onMessageReceived()

if (remoteMessage.getNotification() != null) {
            //Foreground
            Log.d(TAG, "Message Notification Body: " +
                    remoteMessage.getNotification().toString());
            showNotification(remoteMessage.getNotification().getTitle(),
                    String.valueOf(remoteMessage.getNotification().getBody()));
        }
else if (remoteMessage.getData().size() > 0) {
           showNotification(remoteMessage.getData().get("title"),
                    remoteMessage.getData().get("data"));

        }

showNotification()

Intent i = new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0, builder.build());

1 个答案:

答案 0 :(得分:1)

请使用 click_action

例如。

android.manifest文件中

在您注册活动的地方添加以下代码

     <activity
                android:name="your activity name">
                <intent-filter>
                    <action android:name="your activity name" />
                   <category android:name="android.intent.category.DEFAULT"/>
               </intent-filter>
   </activity>

您必须在服务器端注册相同的单击操作。

例如。

$noti = array
    (
    'icon' => 'your_icon_name',
    'title' => 'title',
    'body' => 'msg',
    'click_action' => 'your activity name comes here'
);