Monday, August 13, 2018

HOW TO RECORD AND PLAY AUDIO WITH AVFOUNDATION FRAMEWORK IN IOS?


Hey everyone as we have already created an application using AvFoundation library. Similar to this, we will be using the same library to create our own custom through which we can play and record an audio file. In this tutorial, I am going to walk you through the basics of this framework and show you how to manage audio playing, as well as, recording.

To provide you with an example, I will build a simple audio application that allows users to record and play audio. In this tutorial, my primary focus is to demonstrate the AV Foundation framework so the user interface of our application is very simple.

The AVFoundation provides easy ways to deal with audio. In this tutorial, we will be going to deal with these two classes:

1. AVAudioPlayer – An audio player for playing audio files. By using the player, the user can play sounds of any duration and in any audio format like- mp3 etc available in iOS.

2. AVAudioRecorder – An audio recorder for recording audio within the application.

INITIAL SETUP

First of all, we will be going to run the XCode and then we need to click on the create new application button. Later on, we need to select the single view application as shown below.
Now after this step, we will be going to give a name to our application. For my application, I will name it as “AudioRecordingSample”.

ADDING AVFOUNDATION FRAMEWORK

By default, the AVFoundation framework is not included in any Xcode project. So, we need to add it manually. In the Project Navigator of the application, select the “AudioRecordingSample” project.
In the Content Area, select “AudioRecordingSample” under Targets and click “Build Phases”. Expand “Link Binary with Libraries” and click on the “+” button to add the “AVFoundation.framework” to your project.
Here is the snapshot to which I am adding AVFoundation Framework.
To use the AVAudioPlayer and AVAudioRecorder class and its features, we need to import it in ViewController.h.
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioRecorderDelegate, AVAudioPlayerDelegate>
@end

INTERFACE DESIGNING

Here, we will be going to design a simple yet attractive interface for our application. This will consist of three things here:
  1. A button through which we will be going to play the audio from the application through AV Library and will name it as the Record button. Now set the constraints to it as Top layout, center horizontal, equal width and equal height.
  2. A second button just below the first button through which we will be going to stop the recording after we finish. Here set the constraints to Top to button 1 and center horizontally, equal width and equal height to button 1.
  3. The third button below the second stop button, through which we will be going to record the audio in the application. In this area, set the constraints as top to button second, center horizontally and equal width, equal height with the second button.
After performing all the steps in designing your view will looks similar to this as shown below.

CONNECTIONS WITH OUTLETS

Now we need to create a connection between the outlets and objects with the view controller so that we can use them to perform some functionality later in our app. First, we need to connect all the three buttons simultaneously and then later will going to create their actions in the application as well.
Head to the view controller.h file of the application and add the given line of code to it:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *Play;
- (IBAction)playAction:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UIButton *stop;
- (IBAction)stopAction:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UIButton *record;
- (IBAction)recordAction:(UIButton *)sender;
@end

INITIALIZING RECORDING USING AVAUDIORECORDER

First, let’s take a look how we can use AVAudioRecorder to record audio. Add the AVAudioRecorderDelegate protocol and AVAudioPlayerDelegate in the ViewController.m file. I will be going to explain both delegates as we move forward in the application development.
#import “ViewController.h”
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioRecorderDelegateAVAudioPlayerDelegate>
@end
Next, declare the two variables to handle and enable to recording objects which are AVAudioRecorder and AVAudioPlayer in ViewController.m:
Connect with source url:-

Friday, August 3, 2018

HOW TO SEND PUSH NOTIFICATION IN ANDROID USING C#?


In this blog, I am going to explain how we can integrate a push notification service for Android using C#. We all know that mobile applications are booming in the market. Basically, push notification is used to give updates to application users.
SOME MORE ABOUT PUSH NOTIFICATIONS…
A push notification is a message that pops up on a mobile device. App publishers can send them at any point of time. Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed the app.
Each mobile platform has support for push notifications — iOS, Android, Fire OS, Windows and BlackBerry all have their own services. Push notifications are widely used on all mobile devices to share updated information or events while the user is not actively using the application. On Android gadgets, when a gadget gets a push warning, the sender application’s symbol and a message show up in the status bar. At the point when the client taps the notice, he arrives on the application.
To send push notifications on Android we have to use google GCM. GCM stands for Google Cloud Messaging service. GCM is a free service it can be used to send push notifications to end users. The Google servers involved in taking messages from the third-party application server and sending them to the device.
Google Cloud Messaging is a service that acts as an intermediary between your server and end user’s device. With GCM, Google’s Cloud Connection Server, often referred to as CCS, manages the persistent connections for you. It also makes sure that you push notifications are delivered securely and reliably.
See below picture it describes a pictorial representation of push notification.


To send push notifications, we must have access to below required information:
A.) DeviceId:-This is also known as RegistrationId. It is needed to identify which device the notification has to be sent.
B.) Message: – It contains the notification text.
C.) GoogleAppID: – It is the App Id for the application to be picked for Google project. We must have to register our app on google to get this id. The application that is registering to receive messages. This ensures that the messages are targeted at the correct application.
D.) SenderId: – It is the same as GoogleAppID. The sender ID is used in the registration process to identify an Android application that is permitted to send messages to the device. This ID is typically role-based rather than being a personal account. This ID is generated by the Android developer at user registration time. Save this ID in the database for future use.

GENERATE GOOGLEAPPID
For creating “GoogleAppID” on GCM server end user must have to follow step below.
1.) Log into https://cloud.google.com/console with your Google account.
2.) In the navigation on the left, go into “APIs & Auth > APIs”.
3.) Find “Google Cloud Messaging for Android” and click the “OFF” button next to it.
4.) In the navigation on the left, go into “APIs & Auth > Registered apps”.
5.) Waits a really long time, apparently.
6.) Click the red REGISTER APP button.
7.) Give the app a name and choose Web Application, then click Register.
8.) Click the “Server Key” section for your server key.

BENEFITS OF PUSH NOTIFICATION
Push notifications provide convenience and value to app users. The end user has no need to check their application again and again for a new notification. There are various reasons to use push notifications in your site or app and they have various advantages as far as drawing in with clients and driving activity, notwithstanding, it is basic to utilize them in a way that keeps clients associated, as opposed to irritated.
Push notifications inform users of new information related to your site or an app, providing valuable and relevant updates to customers, even when the site or app is closed. Examples include a message or post on Facebook, a news article update, or a new feature in a game. These are some of the advantages:
1) Fast Delivery
2) Fast Response
3) Location-based Targeting
4) Ease of Use
5) Engaging Users
6) Real Time
For example, users can receive:
A.) Social media notification like what’s app, Facebook and LinkedIn.
B.) Sports scores and news right on their lock screen.
C.) Utility messages like traffic, weather and ski snow reports.
D.) Flight check-in, change, and connection information.

USE SOURCE CODE BELOW:-
Write this code in a separate class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized;

public class PushNotification
{
public PushNotification()
{

//

// TODO: Add constructor logic here

//

}

public string SendPushNotification(string deviceId, string message)

{

string GoogleAppID = “**********************”; //Enter google application id.

var SENDER_ID = “**********************”; //Enter Sender id.

var value = message;

WebRequest tRequest;

tRequest = WebRequest.Create(“https://android.googleapis.com/gcm/send”);

tRequest.Method = “post”;

tRequest.ContentType = ” application/x-www-form-urlencoded;charset=UTF-8″;

tRequest.Headers.Add(string.Format(“Authorization: key={0}”, GoogleAppID));

tRequest.Headers.Add(string.Format(“Sender: id={0}”, SENDER_ID));

string postData = “collapse_key=score_update&time_to_live=108 &delay_while_idle=1&data.message=” + value + “&data.time=” + System.DateTime.Now.ToString() + “®istration_id=” + deviceId + “”;

Console.WriteLine(postData);

Byte[] byteArray = Encoding.UTF8.GetBytes(postData);

tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();

tReader.Close();

dataStream.Close();

tResponse.Close();

return sResponseFromServer;

}

}

Note:-Push notifications are sent through “https://android.googleapis.com/gcm/send” web request.
Call above “SendPushNotification” method from your business logic where the user needs a notification. Use below source code to call above method.

using System;

public class TestPushNotification

{

public TestPushNotification()
{

}

public string SentMessage()

{

PushNotification Obj = new PushNotification();
string result = Obj.SendPushNotification(“17BA0791499DB908433B80F37C5FBC89B870084B”, “Hello Manoranjan”);

return result;

}

}

In above code user must have to pass two parameter device id and messages. See below image. It is demo output of push notification.

CONCLUDING WORDS…

So in this way, we can easily add the push notification features to our apps and get the advantage of this simple but useful feature. I hope this article will help you understand how to implement the Push Notification functionality to your mobile device. Please feel free to use the source code which will help you to integrate.
Connect with source url:-