我正在尝试使用Firebase(在Windows 10上)创建推送通知应用程序,但无法在logcat中获取令牌。实际上,这段代码必须在logcat中显示名为刷新令牌的令牌,但它没有显示它,我也卸载了应用程序,然后多次运行应用程序,但问题仍然是一样的。我不知道我做错了什么,如果有人知道它的解决方案,请张贴在评论中。我已附上所有文件
MainActivity.java
package com.example.mnaum.firebasepushnotification;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.firebase.iid.FirebaseInstanceId;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MyFirebase InstanceIDService.java
package com.example.mnaum.firebasepushnotification;
import android.util.Log;
import android.widget.Toast;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
}
}
MyFirebaseMessagingService。Java语言
package com.example.mnaum.firebasepushnotification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody) {
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 notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
活动_main。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mnaum.firebasepushnotification.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mnaum.firebasepushnotification">
<uses-permission android:name="android.permission.INTERNET"/>
<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>
<!--
Defining Services
-->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
建筑gradle(项目级)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
建筑gradle(应用程序级)
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.mnaum.firebasepushnotification"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.google.firebase:firebase-messaging:9.0.0'
}
apply plugin: 'com.google.gms.google-services'
请参考这个,也许它会有用
调用sendRegstrationToServer(刷新令牌)
在onTokenRe新鲜()
方法中调用this
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
//call this
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
在主活动中。java将其添加到oncreate()中,
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
String token = FirebaseInstanceId.getInstance().getToken();
System.out.println("========== PUSH ID ========" + token);
Toast.makeText(MainActivity.this, "PUSH ID :::" + token, Toast.LENGTH_SHORT).show();
ublic class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
//call this
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
您只需使用扩展FirebaseInstancedService的接收器,就可以在调试日志中搜索带有“Refreshed token:”标记的内容,并且可以看到您的令牌。确保重新启动,因为令牌不会在每次启动应用程序时刷新。
如果您使用的是Wifi网络,可能是谷歌API无法正常工作。如果是这样,请询问管理员,然后您就可以走了。
可能是您没有获取日志,只是尝试复制并通过日志过滤器或IDE中的标记,然后检查日志。
如果你得到然后保存到SharedPrefernce或上传到服务器。
这是舱单 这是我的注册令牌类 这是我的Firebase服务类
我最近注册使用Firebase,但我似乎不知道如何向特定用户推送通知。 对于我来说,我想你可以把它想象成Facebook。每当用户做了某件事,比如发布一篇有趣的文章供其朋友阅读时,我希望该用户的所有朋友都能收到该事件的通知。那么,使用Firebase,如何才能只通知他们的朋友而不是所有人呢?我的数据库存储这些内容。 也许有一种方法可以让每个用户成为Firebase数据库中的一个节点,每次发送通知时
我尝试使用firebase通知控制台发送通知,它工作正常,但在代码中会抛出错误{“multicast_id”:469219618584251399,“success”:0,“failure”:1,“canonical_id”:0,“results”:[{“error”:“InvalidRegistration”}]}我的代码:-
我正在写一个iOS的应用程序,使用laravel的API和谷歌Firebase的推送通知。当我使用Firebase云消息传递发送消息推送时,它会进入我的设备。当我使用laravel发送推送通知时,它不会影响。这里是我的脚本发送推送通知laravel: 它返回一个成功的结果,但通知未发送到iOS设备。 PS:它可以在Android设备上成功运行。
我正在我的应用程序中实施第三部分服务(FreshChat,一种允许您将帮助台集成到应用程序或网站中的服务),除了聊天的推送通知之外,一切都很好。Freshchat通过Firebase工作(Firebase推送通知不会出现问题),但Fresat通知不起作用。我遵循了newchat的官方留档,但有已弃用的方法,绝对不起作用(我尝试了很多) 这是Firebase云消息传递代码: 以及FreshChat文
我正在尝试使用python向所有用户发送推送通知。但是,我知道没有办法使用应用程序做到这一点,您必须使用主题(据我所知)。有没有办法可以从应用程序中创建主题?谢谢编辑:我对Firebase完全陌生(如果我很困难,很抱歉)