Question

I am working on chat application using smack in android. I have a question about showing notification. I am displaying a friend list with status to user after logIn in a ListView. Now I want show to user the notification of number of chats of different users he got, while chatting with the other user on the same activity.

How to achieve this? Please help me. . Any sample code or tutorial will be greatful.

I am using bind service for log in and getting friend list from server.

Was it helpful?

Solution

like that ::->

public class WifiService extends Service {

private final IBinder mBinder = new MyBinder();

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    boolean networkStatus = haveNetworkConnection();
    ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    Notification not;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if(networkStatus == true || netInfo.isConnected() == true){
         not = new Notification(R.drawable.on, "Wi-Fi Connector", System.currentTimeMillis());
    } else {
         not = new Notification(R.drawable.off, "Wi-Fi is not connector", System.currentTimeMillis());
    }


    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Notification.FLAG_ONGOING_EVENT);        
    not.flags = Notification.FLAG_ONGOING_EVENT;
    if(networkStatus == true || netInfo.isConnected() == true){
        not.setLatestEventInfo(this, "Wi-Fi Connector" , "Wi-Fi is connected. You can download data.", contentIntent);
    } else {
        not.setLatestEventInfo(this, "Wi-Fi Connector" , "Wi-Fi is not connected. You can not download data.", contentIntent);
    }

    mNotificationManager.notify(1, not);

    return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class MyBinder extends Binder {
    WifiService getService() {
        return WifiService.this;
    }
}

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top