バックエンドで特定のことが発生したときにユーザーのデバイスに通知を送信するサービスを使用しています。しかし今回は、非アクティビティ クラスでのログイン時 (成功したコードの実行時) です。その場合、アクティビティ以外のクラスから通知を送信するにはどうすればよいですか???
コード:
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);
}
上記の方法は両方とも非活動クラスにあります。
このメソッドは非アクティビティ クラスにあります。上記のコードがトリガーされたときに通知を送信したいだけですが、どうすれば同じことを教えてくれますか??
ありがとう