Kol*_*dar 0 android firebase firebase-cloud-messaging firebase-notifications
我正在尝试设置Android手机从服务器接收通知的系统:对于此任务,我们选择了Firebase.虽然应用程序在后台运行,但一切正常:在我从firebase控制台推送消息后,系统托盘上会显示一条消息,并且在用户单击消息后,Intent会向其发送一条额外的数据Activity.
当应用程序已经处于前台时,我的问题是唯一的.该火力点通知是相当清楚的:如果应用程序是在前台,无论哪个类型的邮件服务器发送,OnMessageReceived不会被调用 ...那么,为什么它不能与我的简单的应用程序?一些注意事项可以帮助您解决此问题:
您可以在下面找到所有使用的源代码:
主要活动
package it.bagozi.ada.tutorial.firebase;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private TextView notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.notification = (TextView) this.findViewById(R.id.notification_setter);
Intent i = this.getIntent();
Log.e(TAG, "Intent from Activity caller: " + i);
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
FirebaseNotificationService
package it.bagozi.ada.tutorial.firebase;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseNotificationService extends FirebaseMessagingService {
private static final String TAG = FirebaseNotificationService.class.getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.e(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// 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.
}
}
Run Code Online (Sandbox Code Playgroud)
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.bagozi.ada.tutorial.firebase">
<service android:name=".FirebaseNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1140 次 |
| 最近记录: |