简体   繁体   English

Android Phonegap App-如何发送通知

[英]Android Phonegap App - How to send notifications

Could anyone possibly offer some advice on how to setup Status bar notifications in Android? 谁能提供一些有关如何在Android中设置状态栏通知的建议?

My skillset is all based around design/front-end dev (hence using phonegap) so I'm a beginner with Eclipse. 我的技能全都基于设计/前端开发人员(因此使用phonegap),因此我是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. 我已阅读了本教程-http: //androidforbeginners.blogspot.com/2010/02/how-to-create-status-bar-notifications.html ,并将代码粘贴到了Android Manifest文件的活动区域。 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? 如果我现在将其编译为APK并将其安装在手机上-现在可以接收通知了吗? 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 最好的保罗

You want status bar notification? 您想要状态栏通知吗? If yes...you are lucky...here's a plugin which I already created for phonegap. 如果是的话...您很幸运...这是我已经为phonegap创建的插件。 Look around for how to embed the external plugin in android. 看看如何在android中嵌入外部插件。

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

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. 您应该安装Android SDK,创建设备的仿真器。 Also it is very useful to install Android ADT - this is a pluging for Eclipse to help to develop Android applications. 安装Android ADT也是非常有用的-这是Eclipse的插件,可帮助开发Android应用程序。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM