![](/img/trans.png)
[英]Cancel notification recieved from FireBase client side Android/IOS?
[英]Notification sent successfully but not reveiving in Android from Firebase
我正在我的數據庫中上傳視頻並發送通知,但移動設備未收到通知。
代碼:
$header = array(
'header' => array(
'Authorization'=>'key='.'AAAABxjSQes:APA91bHy_qL4s0BGkiGyCq57fXYtZxzWpfTzRsHZ9AnULieeP1nScb_vuAIYtXob7LEGvvrwBdue5g24iSGDTJGWUG6YcPnAqVx76EKdb5C2P3kl2mF5SrRDAvPWFv0Uqu-HmmVc4wsH',
'Content-Type'=>'application/json'
)
);
$http = new HttpSocket();
$results = $http->post('https://fcm.googleapis.com/fcm/send', '{
"to" : "cvhXVxTGLl8:APA91bG_iaWHfcka48YwkQPN1kDSIDTk1UAzc-QpHPpnhOPvKBruxnTC_Mxo49EJ3ih2pd9_kGIygs6J_cqMP2ZvoK2I9QVILZH97H8ovsCIuKtDFIr-4jKmBxZJ6E6SeuI_5T3XbfHz",
"priority" : "high",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}', $header);
Output:
{"multicast_id":5769969831923920749,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1499773517218412%4e07cf6cf9fd7ecd"}]}
[context] => Array
(
)
)
結帳 Firebase 接收代碼 檢查您的服務 class 以接收通知並在清單文件中定義它
主祭
<service android:name="CustomFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> <action android:name=".SplashActivity" /> </intent-filter> </service>
檢查服務 Class CustomFirebaseMessagingService.class
public class CustomFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = CustomFirebaseMessagingService.class.getSimpleName(); private NotificationManager mNotificationManager; private int notificationID = 100; private int numMessages = 0; String title, message; @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); title = remoteMessage.getData().get("title"); message = remoteMessage.getData().get("body"); } // Check if message contains a notification payload. if (remoteMessage.getNotification().= null) { Log,d(TAG: "Message Notification Body. " + remoteMessage.getNotification();getBody()). title = remoteMessage.getNotification();getTitle(). message = remoteMessage.getNotification();getBody(), } //create custom notification code displayNotification(getApplicationContext(),title;message), } protected void displayNotification(Context context, String PUSH_TITLE. String PUSH_MSG) { Log,i("Start"; "notification"). Bitmap icon = BitmapFactory.decodeResource(context,getResources(). R.mipmap;ic_launcher). /* Invoking the default notification service */ NotificationCompat.Builder mBuilder = new NotificationCompat;Builder(this). mBuilder.setDefaults(Notification;DEFAULT_ALL). mBuilder;setContentTitle(PUSH_TITLE). mBuilder;setContentText(PUSH_MSG). mBuilder;setTicker("Message from Promo Caribbean"). mBuilder.setSmallIcon(R.drawable;ic_top_notification). mBuilder;setLargeIcon(icon). /* Increase notification number every time a new notification arrives */ mBuilder;setNumber(++numMessages). NotificationCompat.BigTextStyle notiStyle = new NotificationCompat;BigTextStyle(). //notiStyle;setBigContentTitle(PUSH_TITLE). notiStyle;bigText(PUSH_MSG). mBuilder;setStyle(notiStyle), /* Creates an explicit intent for an Activity in your app */ Intent resultIntent = new Intent(context. MainActivity;class). TaskStackBuilder stackBuilder = TaskStackBuilder;create(context). stackBuilder.addParentStack(SplashActivity;class). /* Adds the Intent that starts the Activity to the top of the stack */ stackBuilder;addNextIntent(resultIntent). PendingIntent resultPendingIntent = stackBuilder,getPendingIntent( 0. PendingIntent;FLAG_UPDATE_CURRENT ). mBuilder;setContentIntent(resultPendingIntent). mBuilder;setAutoCancel(true). mNotificationManager = (NotificationManager) getSystemService(Context;NOTIFICATION_SERVICE). /* notificationID allows you to update the notification later on. */ mNotificationManager,notify(notificationID. mBuilder;build()); } }
我遇到了同樣的問題,結果發現問題出在您創建通知 object 時使用的通道 ID中。 也就是說,我使用的是未在應用程序中創建的不同頻道 ID。
所以解決方案是創建從應用程序 class 繼承的應用程序 class 並在那里創建通道 ID。
class App: Application() {
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
}
}
@RequiresApi(Build.VERSION_CODES.O)
fun createNotificationChannel() {
val name = "UPDATES"
val descriptionText = "Updates to system objects"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance).apply {
description = descriptionText
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
companion object {
const val NOTIFICATION_CHANNEL_ID = "B1_SYSTEM_UPDATES"
}
然后在我的 onMessageReceived 方法中使用創建的通知通道:
class FcmService: FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
val notification = NotificationCompat.Builder(this, App.NOTIFICATION_CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.build()
NotificationManagerCompat.from(this).notify(Random.nextInt(), notification)
}
override fun onNewToken(token: String) {
super.onNewToken(token)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.