[英]Android app does not start intent with firebase
我有一個使用 firebase 來顯示通知的應用程序,但我有一個活動,而不是顯示通知,它應該啟動一個活動意圖。
錯誤是在 android 9 版本中
這是我的代碼:
FirebaseMessagingServiceApp:
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
int value = Integer.parseInt(remoteMessage.getData().get("value"));
showNotification(value);
}
}
private void showNotification(int value) {
Intent intent = new Intent(this, NewClassMessage.class);
intent.putExtra("value", value);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
這是我的清單
<activity
android:name=".NewClassMessage"
android:label="Detail"
android:windowSoftInputMode="stateAlwaysHidden"
android:screenOrientation="portrait" />
我究竟做錯了什么? 在較低版本中,活動開始時錯誤在 android 9
提前致謝
嘗試使用 PendingIntent 從通知中打開
Intent notificationIntent = new Intent(this, NewClassMessage.class);
notificationIntent.putExtra("value", value);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
uniqueInt, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
運行 Android 6 及更高版本的設備不會顯示通知,因為您需要為通知管理器設置一個通知渠道。
嘗試這個
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// Map<String,String> data=remoteMessage.getData();
// Toast.makeText(getApplicationContext(),data.get("Firefy"),Toast.LENGTH_LONG);
Map<String,String> data=remoteMessage.getData();
showNotification(data);
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
//Toast.makeText(this, "Message Notification Body: " + remoteMessage.getNotification().getBody(),Toast.LENGTH_LONG);
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
public void showNotification(Map<String,String> dataReceived) {
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), "notify_001");
Intent ii = new Intent(getApplicationContext(), SignUpActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, ii, 0);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Bitmap bitmap = getBitmapfromUrl(dataReceived.get("image"));
mBuilder.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(bitmap)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null)
.setBigContentTitle("hahaha")
.setSummaryText("we are done \n"+dataReceived.get("account")+"="+dataReceived.get("balance")))
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setOnlyAlertOnce(true);
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// === Removed some obsoletes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "hahah Channel";
NotificationChannel channel = new NotificationChannel(channelId,"hahaha notifications",NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("telja notifications keeps you in the loop.");
mNotificationManager.createNotificationChannel(channel);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
mNotificationManager.notify(0, mBuilder.build());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.