Tuesday, 19 November 2013

Android pushNotification badge ICON similar to IOS


1.    Create a appwidget-provider xml name it as widget_badge_icon.xml and add it in xml folder
2.    Create a widget_badge_layout .xml as per need.
3.    Create a receiver in manifest file.
4.    Create BadgeNotificationWidget Class:


package com.example.testwidget;

import java.util.Timer;

import java.util.TimerTask;

import android.app.IntentService;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

/**
 * BadgeService is a worker thread used to send the badge number as a broadcast
 * message to change the app icon.
 *
 *
 */
public class BadgeService extends IntentService {

    int count = 0;

    Timer timer;

    int randomNo = 10;

    public BadgeService() {
        super("BadgeService");
        timer = new Timer();
    }

    /**
     * This method is invoked on the worker thread with a request to process.
     */
    @Override
    protected void onHandleIntent(final Intent intent) {

        RemoteViews remoteView = new RemoteViews(getApplicationContext()
                .getPackageName(), R.layout.widget_badge_layout);

        ComponentName componentName = new ComponentName(
                getApplicationContext(), BadgeNotificationWidgetActivity.class);

        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(getApplicationContext());

        Log.d("BadgeNotificationWidgetActivity", "BadgeService randomNo"
                + randomNo);
        if (randomNo == 0) {
            remoteView.setViewVisibility(R.id.badgeNum, View.GONE);
        } else {
            remoteView.setViewVisibility(R.id.badgeNum, View.VISIBLE);
        }

        remoteView.setTextViewText(R.id.badgeNum, String.valueOf(randomNo));
        // apply changes to widget
        appWidgetManager.updateAppWidget(componentName, remoteView);

    }
}






package com.example.testwidget;




import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

   
   
    @Override
    protected void onStart() {
        super.onStart();
        startService(new Intent(this, BadgeService.class));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}




package com.example.testwidget;

import android.appwidget.AppWidgetManager;

import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import android.widget.RemoteViews;

public class BadgeNotificationWidgetActivity extends AppWidgetProvider {

   

   

   

}





<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testwidget"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/image"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testwidget.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <service android:name=".BadgeService" >
        </service>
       
       
         <receiver
            android:name="com.example.testwidget.BadgeNotificationWidgetActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_badge_icon" />
        </receiver>
    </application>

</manifest>



badge_circle.xml


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <corners android:radius="10dip" />

    <solid android:color="#F00" />

    <stroke
        android:width="5dp"
        android:color="#FFF" />

    <padding
        android:bottom="8dp"
        android:left="10dp"
        android:right="10dp"
        android:top="8dp" />

</shape>

widget_badge_icon.xml


<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_badge_layout"
    android:minHeight="40dp"
    android:minWidth="40dp"
    android:updatePeriodMillis="30000" >

</appwidget-provider>


Following link contains source code   Sample Source

4 comments:

  1. can u post the download link @sonu asher

    ReplyDelete
  2. Hello,
    Please clicl on above link for sample example

    ReplyDelete
  3. Dear i have downloaded this but its not like same code avobe

    ReplyDelete

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...