簡體   English   中英

從非活動類發送通知

[英]Send Notification from a non-activity class

我正在使用一種服務,當后端發生特定情況時,該服務會向用戶設備發出通知。 但是現在這次是在非活動類中登錄(一段成功的代碼運行)。 如何從非活動類中發送通知?

碼:

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

    }

上述方法的兩種方法都屬於非活動類。

此方法屬於非活動類。 我只是想在上面的代碼被觸發時發送通知現在告訴我有關我怎么樣?

謝謝

可能有幾種選擇:

  • checkXMPP方法中添加一個參數:參數將是一個上下文。 這通常是最簡單的,但會導致許多代碼綁定到上下文,從而產生更多耦合。 實際上,在這里你需要為數據包監聽器提供一個上下文。

  • 您也可以啟動自己的IntentService ,它將通過NotificationManager更新通知。 使用Intent啟動服務。 這看起來很難,但事實並非如此。

這是一個例子: http//androidhotel.wordpress.com/2011/12/14/a-simple-notification-system-with-the-intentservice-part-i/

這是我如何做到這一點,像這樣創建一個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());
}

}

並在任何activity.class調用setNotification()函數

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM