英文:
How to open The specific Activity after clicking FCM push notification
问题
我尝试点击推送通知后打开特定活动,以下是代码:
public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
private static final String CHANNEL_ID = "my_notification";
private static final int NOTIFICATION_ID = 1;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("Token:", s);
}
@SuppressLint("MissingPermission")
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// 检查消息是否包含通知负载。
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Log.d("msg:", message);
// 将通知数据传递给NotificationActivity。
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra("title", title);
intent.putExtra("message", message);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
// 构建通知。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
int notificationId = (int) System.currentTimeMillis();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
}
}
}
这是我的AndroidManifest.xml文件的FCM配置:
<service
android:name=".MyFirebaseInstanceIDService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_click_action"
android:value=".NotificationActivity" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
请注意,我只提供了代码的翻译部分,没有包括额外的内容。
英文:
I have tried to open the specific activity after clicking the push notification
Here is the code
public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
private static final String CHANNEL_ID = "my_notification";
private static final int NOTIFICATION_ID = 1;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("Token:", s);
}
@SuppressLint("MissingPermission")
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// Check if the message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Log.d("msg:", message);
// Pass the notification data to the NotificationActivity.
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra("title", title);
intent.putExtra("message", message);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
// Build the notification.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
int notificationId = (int) System.currentTimeMillis();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
}
}
}
Here is is my Androidmanifest.xml for fcm
<service
android:name=".MyFirebaseInstanceIDService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_click_action"
android:value=".NotificationActivity" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
Here iam facing the issue like the NotificationActivity is not opening And the onMessageRecived function is not running in background
答案1
得分: 0
以下是您的代码的中文翻译部分:
public static void sendNotification(Context context, String title, String body,
Map<String, String> data, String userType) {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_my_taxi);
Intent intent = new Intent(context, NotificationActivityDriver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "channel_id")
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setContentInfo(title)
.setLargeIcon(icon)
.setColor(Color.BLUE)
.setLights(Color.BLUE, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.drawable.ic_my_icon);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Manifest文件如下:
<service
android:name="com.notification.test.service.MyFirebaseMessagingService"
android:directBootAware="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
英文:
here is my code.
public static void sendNotification(Context context, String title, String body,
Map<String, String> data, String userType) {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_my_taxi);
Intent intent = new Intent(context, NotificationActivityDriver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "channel_id")
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setContentInfo(title)
.setLargeIcon(icon)
.setColor(Color.BLUE)
.setLights(Color.BLUE, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.drawable.ic_my_icon);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Manifest file is below
<service
android:name="com.notification.test.service.MyFirebaseMessagingService"
android:directBootAware="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论