0

バックエンドで特定のことが発生したときにユーザーのデバイスに通知を送信するサービスを使用しています。しかし今回は、非アクティビティ クラスでのログイン時 (成功したコードの実行時) です。その場合、アクティビティ以外のクラスから通知を送信するにはどうすればよいですか???

コード:

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);

    }

上記の方法は両方とも非活動クラスにあります。

このメソッドは非アクティビティ クラスにあります。上記のコードがトリガーされたときに通知を送信したいだけですが、どうすれば同じことを教えてくれますか??

ありがとう

4

2 に答える 2

1

いくつかのオプションがあります:

  • メソッドにパラメーターを追加します。パラメーターcheckXMPPはコンテキストになります。これは通常最も単純ですが、コンテキストに関連付けられた多くのコードにつながるため、より多くの結合が生じます。実際、ここでは、パケット リスナーにコンテキストを与える必要があります。

  • NotificationManager を介して通知を更新する独自のIntentServiceを開始することもできます。Intent を使用してサービスを開始します。難しそうに見えるかもしれませんが、実際にはそうではありません。

例を次に示します: http://androidhotel.wordpress.com/2011/12/14/a-simple-notification-system-with-the-intentservice-part-i/

于 2012-11-05T07:10:08.360 に答える
0

これが私がこれを行った方法です。この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);
}
于 2018-05-15T07:34:55.943 に答える