在 android 中使用 firebase 获取令牌推送通知

2023-12-11

我想做推送通知,这是我的代码

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(String s) {
        Log.e("NEW_TOKEN", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("FCM NOTIFICATION");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon((R.mipmap.ic_launcher));
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

    }
}

但这不起作用,我认为令牌的问题我已经看到了很多有关获取令牌的问题和答案,但我无法在那里获得有用的信息。我将不胜感激任何答案,提前致谢。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MainActivity.this,  new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {
            String newToken = instanceIdResult.getToken();
            Log.e("newToken",newToken);

        }
    });
}

}


试试这个方法。 1. 创建服务

public class YourService extends FirebaseMessagingService {

public static int NOTIFICATION_ID = 1;

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

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this, "2")
            .setSmallIcon(R.drawable.your_icon)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (NOTIFICATION_ID > 1073741824) {
        NOTIFICATION_ID = 0;
    }
    Objects.requireNonNull(notificationManager).notify(NOTIFICATION_ID++, mNotifyBuilder.build());
}

}

现在将其添加到清单中

<service
   android:name=".YourService"
   android:exported="false">
   <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 android 中使用 firebase 获取令牌推送通知 的相关文章

随机推荐

  • f2py:包装 fortran 模块,该模块利用分布在不同文件中的子例程?

    由于我描述的原因earlier 我需要在Python中使用LAPACKs dgesvd和zgesvd方法 而不是包装在numpy中的方法 有人指出 我可以使用 f2py 来创建我自己的 python 包 问题是 lapack 中的 dges
  • 重写函数类型原型

    我在玩耍并试图覆盖时遇到了一个有趣的行为Function原型 假设我们已经像这样重写了 toString const funcToString Function prototype toString Function prototype t
  • 清理 Oracle 序列

    我广泛使用 SQL Server 但对 Oracle 的经验很少 我接到的任务是 清理 Oracle 数据库中的序列 但我不知道如何安全地完成此任务 我需要确定表中实际的最大值 假设 ID 105 然后查看该 ID 的下一个序列是什么 如果
  • fontello 中的图标字体不适用于 Meteor js

    我已经从 fontello 下载了自定义图标字体 并打算在我的流星应用程序中使用它 我尝试了下载包附带的演示 字体显示正常 这是我的CSS font face font family fontello src url fonts fonte
  • iOS 应用程序:几次崩溃,常见主题为:SIGSEGV 和 _sigtramp

    作为 obj c 的新手 我不擅长阅读堆栈跟踪 但我通常可以找出代码中从哪里开始查找 然而 在测试过程中 其中一位测试人员不断报告多次随机崩溃 我无法确定堆栈跟踪的头或尾 因为它们不指向我自己的任何代码 这里有两个 0 WIT Free 0
  • 如何在 Swift 中将枚举和 switch() 与 UITableViewController 结合使用

    我的 UITableView 有两个部分 所以我为它们创建了一个枚举 private enum TableSections HorizontalSection VerticalSection 如何使用在 numberOfRowsInSect
  • Android Automotive Arm_86 未在虚拟设备中列出

    我一直在开发 Android 汽车应用程序 但由于未知原因尚未设置模拟器 我已经尝试过文档并且this我正在使用金丝雀频道 下载汽车图像后 它没有列在虚拟设备下 我使用的是android studio 4 0 1版本 如何解决这个问题 谢谢
  • 如何在网站上添加实时代码编辑器? [关闭]

    Closed 这个问题需要细节或清晰度 目前不接受答案 我正在创建一个需要实时编辑代码 Java c python javascript 等 的网站 我知道 codemirror 我想知道如何在网站上运行代码 例如 W3Schools 自己
  • 矢量 a 、矢量 a[n] 和矢量 a(n) 之间有什么区别?

    我刚刚了解了向量 但对它们的用途感到困惑 请告诉我以下之间有什么区别 vector
  • 存储过程插入两个有关系的表?

    我试图将新行插入两个有关系的表中 我写的存储过程如下 ALTER PROCEDURE InsertUserProfile UserID varchar 10 Pass varchar 50 Enabled int Permission in
  • 集群模式路由网格不工作,默认情况下像主机模式一样工作

    描述 Swarm 模式路由网格不起作用 而是像默认情况下使用主机模式一样工作 我们部署了由 3 个主节点和 8 个工作节点组成的集群 每个节点都位于云服务的不同实例中开放堆栈使用 Terraform 和 Ansible 群和路由网格工作完美
  • 合并两个表格轨道

    基本上我有同样的问题 比如 user1224344 如何从 Rails 中的同一页面提交多个重复的表单 最好使用一个按钮 第一个答案看起来相当不错 但作为 Rails 初学者 我在将其移植到我的项目中时遇到问题 好的 我有两个具有相同控制器
  • 如何检查用户是否正在刷新页面或导航到其他页面

    场景 每当用户离开页面时 我想在本地存储中设置一些变量 Using window onbeforeunload我收到用户即将离开页面的事件 但我想在用户刷新时设置不同的变量值以及不同的页面导航 var someVar when refres
  • 如何将我的类隐式转换为另一种类型?

    例如隐式地 MyClass myClass new MyClass int i myClass 您需要在MyClass file public static implicit operator int MyClass instance if
  • 向自定义策略添加限制并使声明可选 Azure B2C

    我正在尝试使用 LocalAccountSignUpWithLogonName 自定义策略收集用户详细信息 我已添加
  • 从 Snowflake 发送电子邮件警报

    如果 Snowflake 中的存储过程失败 我们有什么方法可以发送电子邮件警报吗 当我检查 Snowflake 文档时 Snowflake 中没有提到电子邮件实用程序 您可以直接从 Snowflake 发送电子邮件 也可以选择将表 视图中的
  • OperaDriver 无法启动

    Opera 浏览器上周进行了更新 从那时起它就停止工作了 它会打开浏览器并在 URL 中显示 data 并引发 webdriver 异常 Opera 41 与 selenium 2 53 1 兼容吗 抛出的异常是 Result Messag
  • ReactJS 将 HTML 字符串转换为 JSX

    我在处理 facebook 的 ReactJS 时遇到了麻烦 每当我执行 ajax 并想要显示 html 数据时 ReactJS 将其显示为文本 见下图 通过jquery Ajax的success回调函数来显示数据 ajax url url
  • 如何使用 selenium webdriver 和 Java 处理弹出窗口

    请帮助 我是硒的新手 我尝试自动化电子商务网站 但在处理弹出窗口时遇到问题 这是场景 Go to http www lampsplus com 单击标题部分中的销售链接 单击页面上显示的第一个项目 产品 这将显示产品页面 在产品页面上 单击
  • 在 android 中使用 firebase 获取令牌推送通知

    我想做推送通知 这是我的代码 public class MyFirebaseMessagingService extends FirebaseMessagingService Override public void onNewToken