How To Implement The Push Notification In Android
Solution 1:
This is the link to documentation given by the Google. They named the concept of PushNotification as C2DM (Cloud To Device Messaging)
You can get a clear description by visiting the given link. I'll give you some short answere for your questions.
- You can't implement this in Android 1.6. You need 2.2 or higher version
- As PushNotification, we get only alerts, not the full details message.
- As input for the third party server, should have the device registration ID with the C2DM.
- Yes, there should be a device id to identify the device to activate the service. You can get it at the initial phase where your Android app try to connect with the C2DM
Solution 2:
In Firebase we can push notification with multiple pieces of information to the specific or multiple devices for that we need to implement some code from the android side, first, we need to set up the firebase configuration in your app, I will cover how to redirect push notification to specific or default a screen in the mobile application. Two ways to open the application screen
- By default when you only need to open the application(Like splash screen).
- Redirect to the specific screen in the application.
By default when you only need to open the application(Like splash screen) Create a class Named "FirebaseMessagingService" and extends "FirebaseMessagingService"
Code to implement
publicclassFirebaseMessagingServiceextendsFirebaseMessagingService
{
@OverridepublicvoidonNewToken(String token)
{
sendRegistrationToServer(token);
}
publicvoidonMessageReceived(RemoteMessage remoteMessage)
{
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Uri imageUrl = remoteMessage.getNotification().getImageUrl();
String actionItem = remoteMessage.getNotification().getClickAction();
if (imageUrl == null)
{
MyNotificationManager.getmInstance(getApplicationContext()).displayNotificationAction(title, body,actionItem);
}
else
{
MyNotificationManager.getmInstance(getApplicationContext()).displayImageNotification(title, body, imageUrl);
}
}
privatevoidsendRegistrationToServer(String token)
{
// TODO: Implement this method to send a token to your app server.
}
}
Create Notification Manager class to manage the display method with different parameters
publicclassMyNotificationManager
{
private Context mCtx;
privatestatic MyNotificationManager mInstance;
privateMyNotificationManager(Context context)
{
createNotificationChannel();
mCtx = context;
}
publicstatic synchronized MyNotificationManager getmInstance(Context context)
{
if (mInstance == null)
{
mInstance = new MyNotificationManager(context);
}
return mInstance;
}
publicvoidcreateNotificationChannel()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("1", "Testing the Notification", importance);
channel.setDescription("We are testing the notification");
}
}
publicvoiddisplayNotification(String title, String body)
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constant.CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_notification)
.setColor(ContextCompat.getColor(mCtx, R.color.colorPrimary))
.setContentTitle(title)
.setContentText(body);
Intent intent = new Intent(mCtx, SplashActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null)
{
mNotificationManager.notify(1, mBuilder.build());
}
}
publicvoiddisplayImageNotification(String title, String body, Uri imageUrl)
{
NotificationCompat.Builder notification = null;
NotificationManager mNotificationManager = null;
try
{
notification = new NotificationCompat.Builder(mCtx, Constant.CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(title)
.setAutoCancel(true)
.setColor(ContextCompat.getColor(mCtx, R.color.colorPrimary))
.setLargeIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Picasso.with(mCtx).load(imageUrl).get() : Picasso.with(mCtx).load(R.mipmap.ic_notification).get())
.setContentText(body)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(Picasso.with(mCtx).load(imageUrl).get())
.bigLargeIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Picasso.with(mCtx).load(imageUrl).get() : Picasso.with(mCtx).load(R.mipmap.ic_notification).get()));
Intent intent = new Intent(mCtx, SplashActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null)
{
notification.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(1, notification.build());
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
Now just trigger the notification through Firebase console or send through API like:-
{
"to": "device_token",
"priority": "high",
"notification": {
"body": "Happy Coding",
"title": "All things are difficult before they are easy.",
"image":""
},
"data": {
"image":""
}
}
2.Redirect to the specific screen in the application. Open the AndroidManifest.xml and in activity tag you need to define ...
....
<activityandroid:name=".activity.SpedificActivity"android:screenOrientation="portrait"android:theme="@style/AppTheme.NoActionBar" ><intent-filter><actionandroid:name="SpedificActivityNotification" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter></activity>
....
now call the API
{
"to": "device_token",
"priority": "high",
"notification": {
"body": "Happy Coding",
"title": "All things are difficult before they are easy.",
"click_action": "SpedificActivityNotification",
"image":""
},
"data": {
"image":""
}
}
Post a Comment for "How To Implement The Push Notification In Android"