Android 如何在通知单击时打开活动
我使用FCM向用户发送自定义通知,现在我希望当用户单击通知时,活动应该打开 以下是我在mainActivity.java中的代码片段:Android 如何在通知单击时打开活动,android,firebase-cloud-messaging,Android,Firebase Cloud Messaging,我使用FCM向用户发送自定义通知,现在我希望当用户单击通知时,活动应该打开 以下是我在mainActivity.java中的代码片段: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("MyNotification", "MyNotification", Not
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("MyNotification", "MyNotification", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
下面是我的服务级别:
public class MyMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
public void showNotification(String title, String message) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
}
请告诉我如何做到这一点。
提前谢谢
// Create an Intent for the activity you want to start
Intent resultIntent = new Intent(this, ResultActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(resultPendingIntent);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
欲了解更多信息,请访问
编辑
这是你想要的代码
public class MyMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Intent activityIntent = new Intent(getApplicationContext(), YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), pendingIntent);
}
public void showNotification(String title, String message, PendingIntent pendingIntent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
}
欲了解更多信息,请访问
编辑
这是你想要的代码
public class MyMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Intent activityIntent = new Intent(getApplicationContext(), YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), pendingIntent);
}
public void showNotification(String title, String message, PendingIntent pendingIntent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
}
对于单击通知并转到特定活动,应使用挂起的意图。如下图所示:
Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
然后使您的通知生成器如下所示
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
因此,最终的showNotification()
方法如下所示:
public void showNotification(String title, String message, PendingIntent pendingIntent) {
Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
如果您有任何不理解的地方或此处有查询注释。对于单击通知并转到特定活动,您应该使用待定意图。如下图所示:
Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
然后使您的通知生成器如下所示
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
因此,最终的showNotification()
方法如下所示:
public void showNotification(String title, String message, PendingIntent pendingIntent) {
Intent activityIntent = new Intent(getApplicationContext(), SpecificActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
.setContentTitle(title)
.setSmallIcon(R.drawable.noti)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(message);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.notify(999, builder.build());
}
如果您有任何不理解的地方,或者这里有查询注释。我的意思是我应该在哪里创建挂起的意图,showNotification()或onMessageReceived()?您应该在
showNotification()
方法上创建它请查看我发布的代码。在showNotification中,我必须传递parameter,然后在onMessageReceived中,我调用了showNotification()方法。对于挂起的意图
,您不需要传递任何参数。但对于其他人,如您需要字符串标题,字符串消息
您应该传递此消息。但它显示错误,因为我没有传递参数。我的意思是我应该在哪里创建挂起的意图,showNotification()或onMessageReceived()?您应该在showNotification()
方法上创建它请查看我发布的代码。在showNotification中,我必须传递parameter,然后在onMessageReceived中,我调用了showNotification()方法。对于挂起的意图
,您不需要传递任何参数。但对于其他人,如您需要字符串标题、字符串消息
,您应该传递此消息。但它显示错误,因为我没有传递参数