我是新来的通知。我没有找到任何帮助,请帮助我。提前谢谢。我必须在Android应用程序中显示通知但它不会打电话给我的广播接收器,虽然我添加清单中的权限
<Service android:name="com.example.tajgroup.newsapplication.service.NotificationService"
android:enabled="true"/>
<receiver android:name=".reciever.UpdateReciever">
<intent-filter>
<action android:name="android.net.com.CONNECTIVITY_CHANGE" />
</intent-filter>
/></receiver>
and my NotificationService and UpdateReciever are
public class UpdateReciever extends BroadcastReceiver {
ConnectivityManager connectivityManager;
NetworkInfo networkInfo;
String dateString;
public static NotificationManager mNotificationManager;
public static int SIMPLE_NOTFICATION_ID;
static Intent serviceintent;
@Override
public void onReceive(Context context, Intent intent) {
if (!isNetworkAvailable(context)) {
// Notification(context, "Wifi Connection Off");
} else {
serviceintent = new Intent(context, NotificationService.class);
context.startService(serviceintent);
context.stopService(serviceintent);
}
}
// Check for network availability
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
}
public class NotificationService extends Service {
ConnectivityManager connectivityManager;
NetworkInfo networkInfo;
NotificationService notification;
String dateString,Name;
Categories categories;
ArrayList<String> a = new ArrayList<String>();
public static int SIMPLE_NOTFICATION_ID;
private Context _context;
long count, cnt, date1;
int flag = 0;
public static NotificationManager mNotificationManager;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
/*Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG)
.show();*/
}
@Override
public void onStart(Intent intent, int startId) {
//Toast.makeText(getApplicationContext(), "Service Stared", Toast.LENGTH_SHORT).show();
date1 = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
dateString = sdf.format(date1);
System.out.println("Ntification item1 = " + dateString);
//dateString="07/11/2014";
notification();
}
@Override
public void onDestroy() {
//Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
}
private void notification(){
final RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(Config.url)
.build();
MainActivityInterface api = adapter.create(MainActivityInterface.class);
api.Test(
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
BufferedReader reader = null;
String message = "";
String output = "";
try {
reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
output = reader.readLine();
JSONTokener tokener = new JSONTokener(output);
JSONObject json = new JSONObject(tokener);
JSONArray jsonArray = json.getJSONArray("story");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
int id = jsonObj.getInt("id");
String title = jsonObj.getString("title");
String content = jsonObj.getString("content");
String picture = jsonObj.getString("picture");
String saved_file_name = jsonObj.getString("saved_file_name");
String status = jsonObj.getString("status");
String start_date = jsonObj.getString("start_date");
String end_date = jsonObj.getString("end_date");
String created_at = jsonObj.getString("created_at");
String updated_at = jsonObj.getString("updated_at");
categories = new Categories(id,title,content,picture,saved_file_name,status,start_date,end_date,created_at,updated_at);
String[] items = categories.getUpdated_at().split(" ");
String date1, time;
date1 = items[0];
time = items[1];
System.out.println("item = " + updated_at);
System.out.println("noti item1 = " + date1);
if(date1.equals(dateString)) {
notification = new NotificationService();
Name = categories.getTitle();
a.add("\n" + Name);
}
}
notification.Notification(NotificationService.this," \n" + a);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(NotificationService.this, error.toString(), Toast.LENGTH_LONG).show();
}
}
);
}
void Notification(Context context, String message) {
// TODO Auto-generated method stub
// Set Notification Title
String strtitle = context.getString(R.string.app_name);
// Open NotificationView Class on Notification Click
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create Notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
// Set Icon
.setSmallIcon(R.drawable.pen_50px)
// Set Ticker Message
.setTicker(message)
// LED
.setLights(Color.RED, 3000, 3000)
// Set Title
.setContentTitle(context.getString(R.string.app_name))
// Set Text
.setContentText(message)
// Add an Action Button below Notification
.addAction(android.R.drawable.ic_menu_more, "Click to Check", pIntent)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Dismiss Notification
.setAutoCancel(true);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
}
答案 0 :(得分:-1)
可能需要SelfCheckPermission。您正在使用什么API级别?