Dancer
Dancer

Reputation: 17671

Android Phonegap App - How to send notifications

Could anyone possibly offer some advice on how to setup Status bar notifications in Android?

My skillset is all based around design/front-end dev (hence using phonegap) so I'm a beginner with Eclipse.

I have read this tutorial - http://androidforbeginners.blogspot.com/2010/02/how-to-create-status-bar-notifications.html and have pasted the code into the activity area of my Android Manifest file. But I don't quite understand how it will work. If I compile this now as an APK and install it on a phone -- is it now ready to receive notifications? If so how do I send them, and where do I type the sending code?

Hopefully it's fairly simple as my boss is hoping that I'll have it completed before christmas!

Cheers for your help. All the best Paul

Upvotes: 1

Views: 2630

Answers (2)

Ashwarya Gupta
Ashwarya Gupta

Reputation: 45

You want status bar notification? If yes...you are lucky...here's a plugin which I already created for phonegap. Look around for how to embed the external plugin in android.

https://github.com/phonegap/phonegap-plugins/tree/master/Android/StatusBarNotification

Upvotes: 3

Yury
Yury

Reputation: 20936

Here you can find better explanation with source codes about notifications.

Notification can be a reaction on some event. For instance, you can develop a simple application with one button. When you press this button a notification will be displayed in the status bar.

About the development. You should install Android SDK, create an emulator of the device. Also it is very useful to install Android ADT - this is a pluging for Eclipse to help to develop Android applications. After that when you build an application it will be automatically installed on the emulator.

Here is the code how to make a simple notification:

package your.package
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AcNotificationTestMain extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private static final int SEND_ID = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button mBtnSend = (Button) findViewById(R.id.button1);
        mBtnSend.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        Log.v("","OnClick...");
        // Create an object of Notification manager 
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        int icon = android.R.drawable.sym_action_email; // icon from resources
        CharSequence tickerText = "New Notification";   // ticker-text
        long when = System.currentTimeMillis();         // notification time
        Context context = getApplicationContext();      // application Context
        CharSequence contentTitle = "My notification";  // expanded message title
        CharSequence contentText = "Click me!";         // expanded message text

        Intent notificationIntent = new Intent(this, AcNotificationTestMain.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


        mNotificationManager.notify(SEND_ID, notification);
    }
}

And layout file:

<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/>
<Button android:id="@+id/button1" android:text="@string/AcNotificationTest_BtnSendNotificationText" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>

Upvotes: 2

Related Questions