我使用的服务会在后端发生特定情况时向用户设备发出通知。但是现在这次是在非活动类中登录(一段成功的代码运行)。如何从非活动类发送通知?
代码:
public XMPPConnection checkXMPPConnection(String userName,String password) {
connection = new XMPPConnection(connConfig);
try {
connection.connect();
System.out.println("Logginnnngggg innn");
connection.login(userName, password);
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
final PacketCollector collector = connection.createPacketCollector(filter);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
// TODO Auto-generated method stub
//notification(packet.getFrom());
packet = collector.nextResult();
Message message = (Message)packet;
notification(""+message.getBody(), 0);
System.out.println("Messageeeee isisssssss......."+message.getBody());
}
}, filter);
通知代码:
public void notification(CharSequence message, int notificationID) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)UserMenuActivity.context.getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "ABC";
CharSequence contentText = message;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
mNotificationManager.notify(notificationID, notification);
}
上述方法的两个方面都属于非活动类。
此方法属于非活动类。我只是想在上面的代码被触发时发送通知现在告诉我同样的情况我该怎么办?
由于
答案 0 :(得分:1)
可能有以下几种选择:
在checkXMPP
方法中添加参数:参数将是一个上下文。这通常是最简单的,但会导致许多代码绑定到上下文,从而产生更多耦合。实际上,在这里你需要为数据包监听器提供一个上下文。
您也可以启动自己的IntentService,通过NotificationManager更新通知。使用Intent启动服务。这看起来很难,但事实并非如此。
答案 1 :(得分:1)
以下是我如何做到这一点,
像这样创建一个NotificationProvider.java
类
public class NotificationProvider {
public NotificationProvider() {
}
public void setNotification(Context context, String notificationTitle, String notificationMessage, int notificationRequestCode){
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon_mother_and_child_care)
.setContentTitle(notificationTitle)
.setColor(101)
.setContentText(notificationMessage);
Intent intent = new Intent(context, DashboardActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, notificationRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
并在任何setNotification()
中调用activity.class
功能
public class MainActivity extends AppCompatActivity {
private NotificationProvider notificationProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationProvider = new NotificationProvider();
notificationProvider.setNotification(this, "Title", "Message", 1);
}