public class IDLAFirebaseMessagingService extends FirebaseMessagingService
{
public IDLAFirebaseMessagingService() { }
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
Log.d("MF❤️", "From: " + remoteMessage.getFrom());
}
}
RemoteMessage.Notification remoteNotif = remoteMessage.getNotification();
Notification notif = new NotificationCompat.Builder(this,"NotificationChannelID")
.setSmallIcon(R.drawable.chess_knight)
.setContentTitle(remoteNotif.getTitle())
.setContentText(remoteNotif.getBody())
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(999,notif);
//onMessageReceived時發出廣播
Intent intent = new Intent("FCM");
intent.putExtra("notifi",message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
//Activity
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson21);
textView = findViewById(R.id.textView15);
broadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
textView.setText(intent.getStringExtra("notifi"));
}
};
}
@Override
protected void onStart()
{
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter("FCM"));
}
@Override
protected void onStop() {
super.onStop();
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
}
如果之前能收到推播,然後突然收不到了,請檢查AS右下角的Event Log,裡面有紅字的DNS錯誤的話就重開一下
可以
收到推播的(跟iOS相比他們的模擬器簡直是萬能的⚠️⚠️⚠️)Display Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground
Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed
FirebaseInstanceId
.getInstance()
.getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>()
{
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task)
{
if (task.isSuccessful() && task.getResult() != null)
{
Log.d("MF❤️", "FCM token: " + task.getResult().getToken());
}
}
});
Data
messages
可以去 https://github.com/mark33699/IDLA 看一下順便給顆⭐️
如果你喜歡我的影片別忘了按讚分享加訂閱,開啟紅色的小鈴鐺,我們明天見~