我正在尝试开发一个应用程序,其中服务器必须每5秒向运行该应用程序的所有android设备发送通知。我决定使用(谷歌)Firebse云消息发送通知,所以我首先尝试了指南的示例项目
https://firebase.google.com/docs/notifications/android/console-audience
但我不能让它工作。我遵守了所有的指示。我已经发布了我使用的代码。我也做了档案--
当我打开Firebase控制台并发送通知时,我会在日志中看到刷新的令牌(从MyFirebaseInstancedService
类,方法onTokenRefresh()
),但当我再次运行它,并通过复制和粘贴令牌向单个设备发送通知时,不会发生任何事情。另外,当我从控制台向用户段发送通知时,不会再发生任何事情。
我还在同一个网站上为Firebase尝试了相应的ios示例项目,它在iphone上运行良好(所有通知都已发送)。我是否遗漏了什么?
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);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
MyFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
//Toast.makeText(getApplicationContext(),"FROM "+remoteMessage.getFrom(),Toast.LENGTH_LONG).show();
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
//Toast.makeText(getApplicationContext(),"MESSAGE: "+remoteMessage.getData(),Toast.LENGTH_LONG).show();
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
//MainActivity.TestMethod();
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
//Toast.makeText(getApplicationContext(),"MESSAGE: "+remoteMessage.getData(),Toast.LENGTH_LONG).show();
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
sendNotification(remoteMessage.getNotification().getBody());
}
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 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
主要活动:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
Button subscribeButton = (Button) findViewById(R.id.subscribeButton);
subscribeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("news");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button logTokenButton = (Button) findViewById(R.id.logTokenButton);
logTokenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get token
String token = FirebaseInstanceId.getInstance().getToken();
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
以及build.gradle(项目消息传递):
// Top-level build file where you can add configuration options commonto `all sub-projects/modules.`
buildscript {
repositories {
jcenter()
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.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()
mavenLocal()
}
}
以及build.gradle(模块应用程序):
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.google.firebase.quickstart.fcm"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Testing dependencies
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support:support-annotations:24.2.0'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.google.firebase:firebase-messaging:9.4.0'
compile 'com.google.firebase:firebase-core:9.4.0'
compile 'com.google.android.gms:play-services-gcm:9.4.0'
compile 'com.android.support:design:24.2.0'
}
apply plugin: 'com.google.gms.google-services'
...这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.quickstart.fcm">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- [START firebase_service] -->
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- [END firebase_service] -->
<!-- [START firebase_iid_service] -->
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<!-- [END firebase_iid_service] -->
<activity
android:name=".ResultActivity"
android:label="@string/title_activity_result"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
当应用程序处于前台时,您可能正在尝试获取通知。请确保它位于后台,然后重试。否则,当应用程序处于前台时,您需要按照此过程获取通知。
来自Firebase文档:https://firebase.google.com/docs/cloud-messaging/android/receive
Firebase通知的行为因接收应用的前台/后台状态而异。如果希望前台应用程序接收通知消息或数据消息,则需要编写代码来处理onMessageRec的回调。
发送通知消息:https://firebase.google.com/docs/cloud-messaging/android/first-message
发送通知消息
我正在尝试为Web设置Firebase云消息传递。我成功地对其进行了正确初始化并获得了令牌: manifest.json与gcm_sender_id 我可以看到我在控制台中得到令牌,所以我试图验证它,并通过邮递员发送我的第一个通知-这里是留档。 发布网址:https://fcm.googleapis.com/v1/projects/PROJECTID/messages:发送授权:无授权 标题 Bo
我使用此代码通过C#与GCM发送通知消息,使用Winform,Webform,无论什么。现在我想发送到FCM(Firebase云消息传递)。我应该更新我的代码吗?: 但GCM改为FCM。发送通知的代码是否相同?在哪里可以找到服务器API密钥?是相同的解决方案吗?
我正在制作一个android应用程序,其中我使用firebase身份验证来验证用户,使用firestore来存储一些用户数据。我想让用户有一个设置,他们可以订阅推送通知。如果他们订阅,我希望我的应用程序定期向他们发送通知,比如每6小时一次。目前,我已经使用firebase控制台测试了推送通知,它看起来工作正常。以下是我到目前为止的< code > myfirebasemesagingservice
在我们的项目中,我们使用Firebase云消息传递来进行推送通知,我们遇到了消息重复的问题。我们过程如下所示: xamarin.firebase.ios.CloudMessaging 3.1.2 xamarin.firebase.ios.instanceID 3.2.1 xamarin.firebase.ios.core 5.1.3 订阅用户主题推荐 发送主题订阅者请求的通知 null 但是,当用
可以使用第三方服务发送推送通知而不依赖谷歌的Firebase云消息(FCM)吗? 如果应用程序默认不包含Firebase包,那么创建/使用自定义框架是否具有与Firebase相似的功能集?或者Firebase是否以某种方式集成在Android操作系统中,而这种方式是从应用程序外部进行的?
如果一个主题订阅者在向该主题/订阅者发送推送通知时没有在线,那么他/她的消息会丢失还是当他们在线时才收到消息?