Tuesday 16 August 2016

Android Firebase Cloud Messaging(FCM)

Hi,  In this post we will use FCM (Firebase Cloud Messaging). So today in this Firebase Cloud Messaging Tutorial I will teach you how to send push notification to single and multiple android devices using Firebase Cloud Messaging. So lets begin.

In AndroidManifest.xml file Register FCM service

<!-- [START firebase_service] -->
        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <!-- [END firebase_service] -->
        <!-- [START firebase_id_service] -->
        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
        <!-- [END firebase_id_service] --> 

In Builde.gradle

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:9.4.0'
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services' 

In Two Class Files

1. public class FcmNotifyReceiverService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (null != remoteMessage.getData()) {

            sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("message"));

        } else {
            sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }
    }

    private void sendNotification(String tittleStr, String messageBody) {

        Log.w("fcm", "messageBody " + messageBody);

        Intent mIntent = new Intent(this, SplashScreenActivity.class);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        android.support.v7.app.NotificationCompat.Builder b = new android.support.v7.app.NotificationCompat.Builder(this);
        b.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_notify)
                .setTicker("Tittle")
                .setContentTitle(tittleStr)
                .setContentText(messageBody)
                .setAutoCancel(false)
                .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                .setContentIntent(contentIntent)
                .setContentInfo("Tittle Info");

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, b.build());
    }

2.  public class FcmTokenService extends FirebaseInstanceIdService {

        private String serverUrl = null;

        private final String log = "TokenService ";

        // private final String ServerKey = "AIzaSyDgnQHpIPYwGxmcOFBQ2H93ClZPrHrjv_g"; // AIzaSyCwPKoCwWkjui8STXdmORZQAnFdo_otpiA";

        @Override
        public void onTokenRefresh() {

            // Get updated InstanceID token.
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.w("fcm refreshedToken ", refreshedToken);

            sendRegistrationToServer(refreshedToken);
        }

        /**
         * Register new token key to server
         *
         * @param token unique token key
         */
        private void sendRegistrationToServer(final String token) {

        }
    }

Download JSON File from firebase console and add to your project property 
More Refer this link : https://firebase.google.com/docs/cloud-messaging/android/client and 
https://www.simplifiedcoding.net/firebase-cloud-messaging-tutorial-android/


Saturday 2 January 2016

Local Notification Using Broadcast Receiver and AlarmManager



// From Your Activity onCreate method call
NotificationManager.getInstance().RegisterNotification(this);

// Notification Manager Class
public class NotificationManager {

      public static NotificationManager getInstance(){
        return new NotificationManager();;
    }

    public void RegisterNotification(Context context) {

        Intent myIntent = new Intent(context , NotificationReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(
        context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        TimeZone timeZone= TimeZone.getDefault();
        Calendar calendar= Calendar.getInstance(timeZone);
        calendar.set(Calendar.HOUR_OF_DAY, 17);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);

        AlarmManager alarmManager = (AlarmManager)
              context.getSystemService(context.ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
         calendar.getTimeInMillis(), 10 * 1000, pendingIntent);

        //set repeating every 24 hours 24 * 60 * 60 * 1000  
    // alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
  }

    public void sendNotification(Context context) {

        Intent mIntent = new Intent(context, SplashScreenActivity.class);

        mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
          | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(context,
          0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder b = new NotificationCompat.Builder(context);

        b.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.Notify_icon)
                .setTicker("Your Tittle")
                .setContentTitle("Default notification")
                .setContentText("Content of your notification")
                .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
                .setContentIntent(contentIntent)
                .setContentInfo("Info");

        android.app.NotificationManager notificationManager = (android.app.NotificationManager)
         context.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(1, b.build());
    }
}

// For your Receiver Class
public class NotificationReceiver extends BroadcastReceiver {

    @Override    public void onReceive(Context context, Intent intnt) {

        NotificationManager.getInstance().sendNotification(context);
    }
}

// Finally don't forget to mention in your manifest file.

<receiver android:name=".NotificationReceiver" />

Monday 20 July 2015

Android M Features


Which is your favourite Android M feature?


  • Apps asking permission to access features 
  • Google Now just a tap away
  • Improved battery life
  • Android Pay
  • Fingerprint support
  • Power Management
  • New RAM manager
  • Adoptable Storage Devices
  • New App Drawer
  • Auto Backup and Restore for Apps
  • USB Type-CSupport        

 

Android M Developer Preview 2 update: what's new? 


Tuesday 7 July 2015

Android Studio - System Requirements

Windows


  • Microsoft® Windows® 8/7/Vista/2003 (32 or 64-bit)
  • 2 GB RAM minimum, 4 GB RAM recommended
  • 400 MB hard disk space
  • At least 1 GB for Android SDK, emulator system images, and caches
  • 1280 x 800 minimum screen resolution
  • Java Development Kit (JDK) 7
  • Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality.

Mac OS X


  • Mac® OS X® 10.8.5 or higher, up to 10.9 (Mavericks)
  • 2 GB RAM minimum, 4 GB RAM recommended
  • 400 MB hard disk space
  • At least 1 GB for Android SDK, emulator system images, and caches
  • 1280 x 800 minimum screen resolution
  • Java Runtime Environment (JRE) 6
  • Java Development Kit (JDK) 7
  • Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality
On Mac OS, run Android Studio with Java Runtime Environment (JRE) 6 for optimized font rendering. You can then configure your project to use Java Development Kit (JDK) 6 or JDK 7.

Linux


  • GNOME or KDE desktop
  • GNU C Library (glibc) 2.15 or later
  • 2 GB RAM minimum, 4 GB RAM recommended
  • 400 MB hard disk space
  • At least 1 GB for Android SDK, emulator system images, and caches
  • 1280 x 800 minimum screen resolution
  • Oracle® Java Development Kit (JDK) 7
Tested on Ubuntu® 14.04, Trusty Tahr (64-bit distribution capable of running 32-bit applications).

Note : If you read more to get this link http://developer.android.com/sdk/index.html#Requirements