开发者

how to implement the push notification in android

I am doing lots of Research on push notification but i don't understand how to implement in android 1.6. I want to ask what is the requirements for this? which type of information we get from the server end either in tags form or just information? what will be the input or output regarding this.Which input i give to the server and which output come开发者_Go百科s from the server. is there any device id to be require for this? Please suggest me Thanks .


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


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

  1. By default when you only need to open the application(Like splash screen).
  2. 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

        public class FirebaseMessagingService extends FirebaseMessagingService
        {
            @Override
            public void onNewToken(String token)
            {
                sendRegistrationToServer(token);
            }

            public void onMessageReceived(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);
                }
            }

            private void sendRegistrationToServer(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

        public class MyNotificationManager
        {
            private Context mCtx;
            private static MyNotificationManager mInstance;

            private MyNotificationManager(Context context)
            {
                createNotificationChannel();
                mCtx = context;
            }

            public static synchronized MyNotificationManager getmInstance(Context context)
            {

                if (mInstance == null)
                {
                    mInstance = new MyNotificationManager(context);
                }
                return mInstance;
            }

            public void createNotificationChannel()
            {
                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");
                }
            }

            public void displayNotification(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());
                }

            }

            
            public void displayImageNotification(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 ...

        ....
                <activity
                    android:name=".activity.SpedificActivity"
                    android:screenOrientation="portrait"
                    android:theme="@style/AppTheme.NoActionBar" >
                    <intent-filter>
                        <action android:name="SpedificActivityNotification" />
                        <category android: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":""
           }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜