我正在使用一種服務,在後端發生特定事件時向用戶設備發出通知。但是現在這一次在非活動類中登錄時(一段代碼運行成功)。我該如何從非活動類發送通知?發送來自非活動類別的通知
代碼:
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);
}
上述兩種方法都是在非活動類。
此方法處於非活動類。我只是想在上面的代碼被觸發時發送通知,現在告訴我關於相同的問題。
感謝
絕對不清楚。更好地解釋並提供代碼,評論以使我們明白你想要做什麼。 – Snicolas
@Snicolas代碼添加。請看看 –