我希望在通知到来时无需点击通知即可显示
如何在不点击通知的情况下打开
我正在尝试使用以下代码,但它不能打开
活动
。我正在使用FCM
发送通知。如何在不点击通知的情况下打开
活动
?我正在尝试使用以下代码,但它不能打开
活动
:public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
String messageContent = "";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
messageContent = remoteMessage.getData().get("message");
showNotification(remoteMessage.getData().get("message"));
postToastMessage(remoteMessage.getData().get("message"));
Log.d(TAG, "From: " + remoteMessage.getFrom());
}
public void postToastMessage(final String message) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), message +" received", Toast.LENGTH_LONG).show();
Intent newIntent = new Intent(MyFirebaseMessagingService.this, CallActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
}
});
}
private void showNotification(String message) {
Intent i = new Intent(this, CallActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle(" Test")
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
我遇到了这个错误。
FA: Discarding data. Failed to send event to service
onTokenRefresh()
中,你将会收到什么类型的通知? - Stanislav BondarshowNotification("hi")
和postToastMessage("hi")
)。FirebaseInstanceIdService
仅处理令牌刷新事件,而FirebaseMessagingService
将使您的应用程序接收从控制台或其他适当方式发送的消息。 - Jay