我试图使用FCM将通知实现到我的应用程序,并且遇到了一些问题。当应用程序处于前台时,不会出现任何问题,并且按预期显示通知。但是,如果应用程序在后台,通知将不会显示。仅当应用程序处于前台时才会收到Android通知
我的清单看起来像这样:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-sdk android:minSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<activity android:name=".MainActivity" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
<meta-data
android:name="io.fabric.ApiKey"
android:value="xxx" />
</application>
</manifest>
我的服务类:
package pw.ckies.snakegame;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
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;
import java.util.Random;
public class NotificationService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = "Empty";
String message = "Emptyyy";
if(remoteMessage.getNotification() != null) {
if (remoteMessage.getNotification().getTitle() != null) {
title = remoteMessage.getNotification().getTitle();
}
if (remoteMessage.getNotification().getBody() != null) {
message = remoteMessage.getNotification().getBody();
}
}
Log.e("notification","recieved");
sendNotification(title, message);
}
private void sendNotification(String title, String message) {
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_people_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
R.mipmap.ic_launcher))
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(getRequestCode(), notificationBuilder.build());
}
private static int getRequestCode() {
Random rnd = new Random();
return 100 + rnd.nextInt(900000);
}
}
而且当通知收到logcat的说:
07-01 23:09 :14.050? W/GCM-DMM:广播意图回调:结果=取消forIntent {行动= com.google.android.c2dm.intent.RECEIVE FLG = 0x10000000的PKG = XXX(有演员)}
我不知道是什么这意味着我搜索了它并发现了许多有相同问题的人。有人说重新安装Android Studio会解决这个问题,所以我这样做了,并且没有任何运气地更新到2.3.3的最新版本。 我一直在android 7.1.1上测试OnePlus 3。
我build.grade(应用模块):
...
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile "com.google.android.gms:play-services-games:${gms_library_version}"
compile project(':BaseGameUtils')
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:support-annotations:24.0.0'
compile 'com.google.firebase:firebase-ads:11.0.1'
compile 'com.google.firebase:firebase-core:11.0.1'
compile 'com.google.firebase:firebase-messaging:11.0.1'
compile('com.crashlytics.sdk.android:crashlytics:[email protected]') {
transitive = true;
}
}
apply plugin: 'com.google.gms.google-services'
我的后端是使用下面的数据一个python post请求:
{
"notification": {"title": "Title goes here", "body": "Body goes here" },
"to": token
}
我还试图从仪表板火力发送通知。
有人还建议一个未签名的APK不会允许我不确定的背景通知?但是因为我的应用在Play商店中并不存在任何问题。
我已经测试了一切我能想到但我找不到解决方案!这可能是我的电话,也可能只是一个愚蠢的错误。任何帮助表示赞赏!
当应用程序在后台的通知是由系统托盘 – Killer
@Shubham处理是什么意思呢?它不应该仍然显示? –
只有当通知有效载荷包含标题和正文(可选)数据密钥时才会显示 – Killer