如何从showNotification()函数启动片段而不是活动(意图)?



我正在使用Firebase Cloud 消息传递从服务器向移动设备发送消息,因此我创建了一个从FirebaseMessagingService扩展并覆盖onMessageReceived(RemoteMessage remoteMessage)的类,我从中调用实现的方法notifyUser(String from, String notification)。反过来,此方法调用属于我在其中创建通知的类myNotificationManager的另一个方法showNotification(String from, String notification, Intent intent)。问题是我需要启动一个片段而不是一个活动。关于如何做到这一点的任何想法? 提前谢谢。

public class ServicioFirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage){   
notifyUser(remoteMessage.getFrom(),remoteMessage.getNotification().getBody());}
public void notifyUser(String from, String notification){
//Create an object of the class that contain the method which creates the notification
MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
//Create an Intent that will be passed as a parameter in the method that creates the
//notification. This intent will be executed when user clicks on the notification. ShowMessage is a class in which 
//I define some TextView and other elements to show the message
Intent intent = new Intent(getApplicationContext(),ShowMessage.class); 
//put some values in the intent
//.......................
//calls the method that creates the notification
myNotificationManager.showNotification(from,notification,intent);
}
}

MyNotificationManager的代码:

public class MyNotificationManager {
//Activity context
private Context ctx;
//Notification id.
public static final int NOTIFICATION_ID = 234;
//Sound for notification
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//Constructor
public MyNotificationManager(Context ctx) {
this.ctx = ctx;
}
public void showNotification (String from, String notification, Intent intent){
//Define an Intent and actions to perform when called
PendingIntent pendingIntent = PendingIntent.getActivity(
ctx,
NOTIFICATION_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//To configre the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
Notification mNotification = builder.setSmallIcon(R.mipmap.ic_launcher)
//define notification parameters
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(from)
.setContentText(notification)
.setSound(uri)
.setSmallIcon(R.mipmap.ic_face)
.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(),R.mipmap.ic_face))
.build();
//Define notification flags
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
//Notify user about received message
NotificationManager notificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
//Execute notification
notificationManager.notify(NOTIFICATION_ID,mNotification);
}
}//end of class

我已经找到了一种方法。我不是打算调用 ShowMessage.class,而是调用 MainActivity.class。现在,MainActivity(一般的应用程序)可以通过两种方式启动:

  1. 对应用程序的默认访问权限:用户点击应用程序的图标并访问应用程序(主活动)。

  2. 用户点击通知并通过在 ServicioFirebaseMessaging 类中实现的 Intent(String from,String notification) 方法访问应用程序 (MainActivity),该方法扩展了 FirebaseMessagingService。在此意图中,我将传递一些将在 MainActivity 上采用的值。

现在的区别在于,我已经在 MainActivity 中实现了额外的操作,通过内部的 try/catch with 和"if"条件四舍五入,以一种方式,如果用户以第一种方式访问应用程序,则没有执行 Intent,那么没有数据可以从 Intent 中获取,因为它甚至不存在(getIntent().getExtras.getSomething() 将失败), 由于此操作由 try/cath 四舍五入,因此应用不会下降。如果用户以第二种方式访问应用程序,则 try/catch 内部的操作将发生,因为"if"条件将为真,并且在这组操作中,执行对相关片段 (FragmentSelectedMessage) 的调用,将意图的值传递给片段。当然,现在片段有一个支持它的活动。

相关内容

  • 没有找到相关文章

最新更新