yoz*_*zzy 6 android firebase firebase-cloud-messaging
我在Android应用中实施了Firebase推送通知.我实现了两个服务来注册令牌,并在检测到时发出通知.当我的应用程序启动时它正在工作,但当我的应用程序关闭时,它无法正常工作.
public class FirebaseinstanceIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e("Firebase", refreshedToken);
MainActivity.setFirebaseToken(refreshedToken);
}
}
public class MyFirebaseMessageService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.e(TAG, "From: " + remoteMessage.getFrom());
Log.e(TAG, "Notification Message Body: " + remoteMessage.getData().get("title"));
//Calling method to generate notification
sendNotification(remoteMessage.getData());
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(Map<String, String> notification) {
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(notification.get("title"))
.setContentText(notification.get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
我的清单:
<service
android:name=".FirebaseinstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
我的要求是:
{
"to": "ex1CtVz5bbE:APA91bHiM_BCun62A9iCobF1yV26Dwn-hYCDhkYPoWEG5ZdqH0P28UsE5q1v7QibwAX7AJ290-9en9L6f548_2b7WEbJ8RPEWlIotLczjjCP7xEOWqeJk6Iz44vilWYvdu4chPwfsvXD",
"data": {
"title": "Notification",
"body": "Message",
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在StackOverflow中找到了一些解决方案,但它在我的情况下不起作用.我有一个API Rest,它调用API Request Post:https://fcm.googleapis.com/fcm/send
所以,我的问题是:当应用关闭时Firebase会处理推送通知吗?如何操作?
谢谢您的回答
关闭应用时,FCM会支持通知.您的代码似乎没问题,所以我认为电池节能器(或节电器)可以终止您的通知.我在Asus Zenfone上遇到过这样的问题,在使用华为和小米的情况下也有报道.只需禁用它们或在例外列表中添加您的应用程序(如果有).在Android的最新版本中还有一种新的省电模式,也尝试禁用它.
| 归档时间: |
|
| 查看次数: |
2290 次 |
| 最近记录: |