热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在AndroidOreo版本中没有收到FCM通知消息?

如何解决《在AndroidOreo版本中没有收到FCM通知消息?》经验,为你挑选了2个好方法。

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

的Manifest.xml




    
        
            
                

                
            
        
        
            
                
            
            
                
            
        

        

        

        

        

        

    


应用程序/ 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() {
            @Override
            public void onSuccess(InstanceIdResult instanceIdResult) {
                String newToken = instanceIdResult.getToken();
                Log.e("newToken",newToken);
            }
        });
    }
}

Vishal Thakk.. 7

当您定位到Android 8.0(API级别26)时,您必须实现一个或多个通知渠道.如果您的targetSdkVersion设置为25或更低,当您的应用在Android 8.0(API级别26)或更高版本上运行时,其行为与运行Android 7.1(API级别25)或更低版本的设备上的行为相同.

注意: 如果您定位Android 8.0(API级别26)并在未指定通知通道的情况下发布通知,则不会显示通知,系统会记录错误.

注意:您可以在Android 8.0(API级别26)中启用新设置,以显示屏幕警告,当针对Android 8.0(API级别26)的应用尝试在没有通知渠道的情况下发布时,该警告会显示为吐司.要打开运行Android 8.0(API级别26)的开发设备的设置,请导航到"设置">"开发人员选项"启用"显示通知通道警告".

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)

以下代码段将向您展示如何创建通知 -

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() - 允许您设置长按菜单中显示的数字 setChannelId() - 允许您在使用旧构造函数setColor()时显式设置通道ID - 允许RGB值为您设置颜色主题notification setBadgeIconType() - 允许您设置要在长按菜单中显示的图标

有关详细信息,请查看示例



1> Vishal Thakk..:

当您定位到Android 8.0(API级别26)时,您必须实现一个或多个通知渠道.如果您的targetSdkVersion设置为25或更低,当您的应用在Android 8.0(API级别26)或更高版本上运行时,其行为与运行Android 7.1(API级别25)或更低版本的设备上的行为相同.

注意: 如果您定位Android 8.0(API级别26)并在未指定通知通道的情况下发布通知,则不会显示通知,系统会记录错误.

注意:您可以在Android 8.0(API级别26)中启用新设置,以显示屏幕警告,当针对Android 8.0(API级别26)的应用尝试在没有通知渠道的情况下发布时,该警告会显示为吐司.要打开运行Android 8.0(API级别26)的开发设备的设置,请导航到"设置">"开发人员选项"启用"显示通知通道警告".

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)

以下代码段将向您展示如何创建通知 -

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() - 允许您设置长按菜单中显示的数字 setChannelId() - 允许您在使用旧构造函数setColor()时显式设置通道ID - 允许RGB值为您设置颜色主题notification setBadgeIconType() - 允许您设置要在长按菜单中显示的图标

有关详细信息,请查看示例



2> aanshu..:

"从Android 8.0(API级别26)开始,所有通知都必须分配给频道,否则不会出现."

现在必须将个别通知放入特定频道.(参考)

选项1 [简单] 更改目标Android版本Android 7.1(API级别25)或更低.

compileSdkVersion 25
    defaultConfig {
        applicationId "com.fcm"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

选项2 如果您不想更改目标版本,请按照以下方法操作

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     NotificationChannel nc = new NotificationChannel(“[enter your product id]”, “[Name]”,NotificationManager.IMPORTANCE_MAX);
     nc.setDescription(“[your description for the notification]”);
     nc.enableLights(true);
     nc.setLightColor(Color.GREEN);
     nm.createNotificationChannel(nc);
  }

使用以下Builder构造函数

NotificationCompat.Builder notificatiOnBuilder= new NotificationCompat.Builder(appContext, [id you mentioned above in constructor of NotificationChannel])

从Builder创建通知

nm.notify("0", notificationBuilder.build())


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了如何使用elementui分页组件进行分页功能的改写,只需一行代码即可调用。通过封装分页组件,避免在每个页面都写跳转请求的重复代码。详细的代码示例和使用方法在正文中给出。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
author-avatar
谜乱凡_134
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有