gpt4 book ai didi

android - 安卓奥利奥版收不到FCM通知信息?

转载 作者:IT老高 更新时间:2023-10-28 23:13:10 26 4
gpt4 key购买 nike

我已从服务器向用户发送 FCM 通知。它工作正常(直到 api 25)但在奥利奥中,当应用程序没有在后台(服务已关闭)(或)完全关闭时。在这种情况下我没有收到任何 FCM 通知,但在 Whatsapp 中工作正常。这里我附上了FCM代码

list .xml

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />

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

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="fcm"/>

<meta-data android:name="firebase_messaging_auto_init_enabled"
android:value="false" />

<meta-data android:name="firebase_analytics_collection_enabled"
android:value="false" />

</application>

</manifest>

应用程序/gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.fcm"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-messaging:17.1.0'
}

apply plugin: 'com.google.gms.google-services'

MyFirebaseMessagingService.java

package com.fcm;

import android.app.Service;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService
{

@Override
public void onNewToken(String s)
{
super.onNewToken(s);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.e("FCM Message Received","You Have FCM Message");
}
}

MainActivity.java

package com.nexge.fcm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
Log.e("newToken",newToken);
}
});
}
}

最佳答案

当您面向 Android 8.0(API 级别 26) 时,您必须实现一个或多个通知 channel 。如果您的 targetSdkVersion 设置为 25 或更低,当您的应用在 Android 8.0(API 级别 26)或更高版本上运行时,它的行为与在运行 Android 7.1(API 级别 25)或更低版本的设备上相同。

注意:如果您以 Android 8.0(API 级别 26) 为目标并在未指定通知 channel 的情况下发布通知,则不会出现通知并且系统会记录错误。

注意:您可以在 Android 8.0(API 级别 26)中开启一项新设置,以在针对 Android 8.0(API 级别 26)的应用程序时显示屏幕上的警告,该警告显示为 toast 尝试在没有通知 channel 的情况下发布。要为运行 Android 8.0(API 级别 26)的开发设备启用设置,请导航至设置 > 开发者选项启用显示通知 channel 警告。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "id_product";
// The user-visible name of the channel.
CharSequence name = "Product";
// The user-visible description of the channel.
String description = "Notifications regarding our products";
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(mChannel);
}

在 Android Oreo 上创建推送通知

要创建通知,您将使用 NotificationCompat.Builder 类。之前使用的构造函数只接受Context作为参数,但是在Android O中,构造函数是这样的——

NotificationCompat.Builder(Context context, String channelId)

以下代码 fragment 将向您展示如何创建通知 –

Intent intent1 = new Intent(getApplicationContext(), Ma

inActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 123, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),"id_product")
.setSmallIcon(R.drawable.flatpnicon) //your app icon
.setBadgeIconType(R.drawable.flatpnicon) //your app icon
.setChannelId(id)
.setContentTitle(extras.get("nt").toString())
.setAutoCancel(true).setContentIntent(pendingIntent)
.setNumber(1)
.setColor(255)
.setContentText(extras.get("nm").toString())
.setWhen(System.currentTimeMillis());
notificationManager.notify(1, notificationBuilder.build());

Android O 为您提供了更多自定义通知的功能 –

setNumber() – allows you to set the number displayed in the long-press menu setChannelId() – allows you set the Channel Id explicitly if you are using the old constructor setColor() – allows a RGB value to put a color theme for your notification setBadgeIconType() – allows you to set an icon to be displayed in the long-press menu

更多信息check example here

关于android - 安卓奥利奥版收不到FCM通知信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51403886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com