FirebaseMessaging:未找到启动应用的活动

时间:2019-05-17 12:23:13

标签: android firebase firebase-cloud-messaging android-manifest

当我的应用程序处于后台但无法正常运行时,我正在尝试从FireBase正确接收消息。我已经看到了一些Stackoverflow问题,例如我的问题,但是什么也不起作用。 有人可以帮我找到问题吗?

当我的应用程序处于后台但无法运行时,我需要这样做。

此处AndoridManifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />

<uses-feature android:name="android.hardware.type.watch" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault"
    tools:targetApi="ice_cream_sandwich">


    <service
        android:name=".MyfirebaseMessagingService" android:exported="false" >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service
        android:name=".MyFirebaseInstanceIDService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

    <uses-library
        android:name="com.google.android.wearable"
        android:required="true" />
    <!--
           Set to true if your app is Standalone, that is, it does not require the handheld
           app to run.
    -->
    <meta-data
        android:name="com.google.android.wearable.standalone"
        android:value="true" />

    <!-- [START fcm_default_channel] -->
    <!-- [START fcm_default_channel] -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />


    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

            <!--<action android:name="android.intent.action.VIEW" />-->

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="gizmos"
                android:scheme="example" />
        </intent-filter>
    </activity>



</application>

MyfirebaseMessagingService.java:

   public class MyfirebaseMessagingService extends        FirebaseMessagingService {
       public static String TAG = "MyFirebaseMsgService";

       private LocalBroadcastManager broadcaster;



@Override
public void onCreate() {
    super.onCreate();
    createNotificationChannel();

    broadcaster = LocalBroadcastManager.getInstance(this);
}

@Override
public void onDestroy() {
    super.onDestroy();
}
private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = getString(R.string.default_notification_channel_id);

        CharSequence name = getString(R.string.default_notification_channel_id);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(channelId, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Intent home = new Intent(getApplicationContext(), MainActivity.class);
    home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(home);

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

        Intent intent = new Intent("Data");
        intent.putExtra("messageFromCloud", remoteMessage.getNotification().getBody());
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        broadcaster.sendBroadcast(intent);
        if (intent.getExtras() != null)
        {
            RemoteMessage.Builder builder = new RemoteMessage.Builder("MyFirebaseMessagingService");

            for (String key : intent.getExtras().keySet())
            {
                builder.addData(key, intent.getExtras().get(key).toString());
            }

            onMessageReceived(builder.build());
        }
    }


}
   }

2 个答案:

答案 0 :(得分:1)

如果您想将MainActivity用作Activity活动的Notification click,则需要编写如下代码:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />

        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="gizmos"
            android:scheme="example" />
    </intent-filter>
</activity>

它将自动重定向到MainAcitivity上的Notification click

答案 1 :(得分:0)

您需要在清单中提及消息文件,但这不仅仅是通过通知来打开活动的解决方案,您需要在消息文件中写入意图。

  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

对于清单,请编写以下代码:

<meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />



<service
            android:name=".MyFirebaseMessagingService"
            android:permission=""
            tools:ignore="ExportedService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

您需要将其声明为服务,因为通知是服务的一部分,并在应用程序关闭时在应用程序后台运行。