Android 通知 Firebase Cloud

标签 android android-activity push-notification firebase-cloud-messaging background-foreground

我有一个使用 Android Studio 的应用程序和一部使用 Android 9 的手机,我也使用 firebase 云消息传递。

当发送消息时,到达电话,但如果应用程序在后台休眠而不打开仅 MainActivity 的 Activity 。

我有那个实现:

implementation 'com.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-messaging:17.5.0'
    implementation 'com.google.firebase:firebase-database:16.0.4'
    implementation 'com.google.firebase:firebase-storage:16.0.4'
    implementation 'com.google.firebase:firebase-firestore:17.1.2'

这是我在 Android 上的 Java 代码

private final String ADMIN_CHANNEL_ID ="admin_channel";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    NotificationManager notificationManager =
            (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    int notificationID = new Random().nextInt(3000);

  /*
    Apps targeting SDK 26 or above (Android O) must implement notification channels and add its notifications
    to at least one of them. Therefore, confirm if version is Oreo or higher, then setup notification channel
  */
  System.out.println("Sdk " + Build.VERSION.SDK_INT);
    Notification.Builder notificationBuilder = null;
    final Intent intent = new Intent(this, DashboardActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK );
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

    Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //setupChannels(notificationManager);
        CharSequence name = "New notification";
        String description = "Device to devie notification";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(ADMIN_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        notificationManager.createNotificationChannel(channel);

        notificationBuilder = new Notification.Builder(this, ADMIN_CHANNEL_ID)
                .setSmallIcon(R.drawable.logopqno)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getData().get("message"))
                .setAutoCancel(true)
                .setSound(notificationSoundUri)
                .setOnlyAlertOnce(false)
                .setChannelId(ADMIN_CHANNEL_ID)
                .setContentIntent(pendingIntent);

    } else {
        notificationBuilder = new Notification.Builder(this, ADMIN_CHANNEL_ID)
                .setSmallIcon(R.drawable.logopqno)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getData().get("message"))
                .setAutoCancel(true)
                .setSound(notificationSoundUri)
                .setOnlyAlertOnce(false)
                .setContentIntent(pendingIntent);
    }
    //Set notification color to match your app color template
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        notificationBuilder.setColor(getResources().getColor(R.color.colorPrimaryDark));
    }
    notificationManager.notify(notificationID, notificationBuilder.build());
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void setupChannels(NotificationManager notificationManager){
    CharSequence adminChannelName = "New notification";
    String adminChannelDescription = "Device to devie notification";

    NotificationChannel adminChannel;
    adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID,
            adminChannelName, NotificationManager.IMPORTANCE_HIGH);
    adminChannel.setDescription(adminChannelDescription);
    adminChannel.enableLights(true);
    adminChannel.setLightColor(Color.RED);
    adminChannel.enableVibration(true);
    if (notificationManager != null) {
        notificationManager.createNotificationChannel(adminChannel);
    }
}

这是我的 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dist="http://schemas.android.com/apk/distribution"
    package="com.machinecare.app">

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

    <dist:module dist:instant="true" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- Added by @BM since 08 2019 Nov -->
    <uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".notification.DashboardActivity"
            android:label="@string/title_activity_dashboard"
            android:parentActivityName=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".notification.MyFirebaseMessagingService"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service android:name=".notification.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest>

最佳答案

在 FCM 中将 Click_action 设置为您要打开的 Activity 。

{
"to": "some_device_token",
"content_available": true,
"notification": {
    "title": "hello",
    "body": "yo",
    "click_action": "Notification_activity" // for intent filter in your activity
},

"data": {
    "extra": "juice"
}

在 Manifest.xml 中添加 intent-filter

关于Android 通知 Firebase Cloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449501/

相关文章:

java - Activity 在android中动态加载xml布局

iphone - 推送通知不起作用?

android - 如何禁用 GridView 的滚动

Android - 检查应用内购买是否已经完成

android - 如何在应用程序中使用flurry?

android - 如何从 TextView 启动 Activity?

android - 为什么在 Android 上不调用 OnOptionsMenuClosed

android - 如何在从布局 : main. xml 初始化的类中调用 startActivityForResult

Android - GCMRegistrar.checkDevice() 的 UnSupportedOperation 异常

firebase - FCM Go Admin SDK 提供 MismatchSenderId