1

我正在開發具有推送通知功能的應用程序。我跟着以下鏈接作爲Android Push NotificationAndroid推送通知:點擊通知獲取數據,存儲和顯示新活動

我試着通過在generateNotification()的代碼中進行以下更改,通過單擊通知成功發送URL並打開網頁。

/** 
* Issues a notification to inform the user that server has sent a message. 
*/ 
private static void generateNotification(Context context, String message) { 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, "Message received", System.currentTimeMillis()); 
    // Hide the notification after its selected 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    //adding LED lights to notification 
    notification.defaults |= Notification.DEFAULT_LIGHTS; 

    Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setData(Uri.parse(message)); 
    //startActivity(browserIntent); 

    //PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
    notification.setLatestEventInfo(context, "Message", "New message received", pendingIntent); 
    notificationManager.notify(0, notification); 

我可以通過服務器的推送通知發送數據。 現在我要執行以下任務:

  1. 通過推送通知發送JSON數據。

  2. 將數據保存到SQLite數據庫中。

  3. 打開點擊推送通知的新活動。

  4. 顯示來自推送新活動通知的數據。

  5. 如果應用程序已關閉,所以在點擊通知後,應用程序就開始了。

因此,請指導我執行上述任務時應遵循的步驟。

+5

你想在旁邊穿衣服嗎? –

回答

8

我解決了問題,如:

  1. 通過推送通知發送JSON數據。 A.能夠在4KB大小的PHP JSON服務的幫助下從SERVER發送數據。

  2. 將數據保存到SQLite數據庫中。 A.保存的數據SQLite中,當數據來自推送通知中的onMessage()

    protected void onMessage(Context context, Intent intent) { 
        Log.i(TAG, "Received message"); 
        String message = intent.getExtras().getString("price"); 
        Log.d("OnMSG",message); 
    
        displayMessage(context, message); 
    
        DataBaseHelper dataBaseHelper = new DataBaseHelper(context); 
        dataBaseHelper.openDataBase(); 
        dataBaseHelper.insertData(message); 
        dataBaseHelper.close(); 
    
        // notifies user 
        generateNotification (context, message); 
    } 
    
  3. 打開新的活動上的推送通知點擊。 答:我使用掛起的意圖在onMessage()中調用生成通知函數來完成此操作。

    private static void generateNotification(Context context, String message) { 
        int icon = R.drawable.ic_launcher; 
        long when = System.currentTimeMillis(); 
        NotificationManager notificationManager = (NotificationManager) 
         context.getSystemService(Context.NOTIFICATION_SERVICE); 
        Notification notification = new Notification(icon, message, when); 
    
        String title = context.getString(R.string.app_name); 
    
        Intent notificationIntent = new Intent(context, MainActivity.class); 
        notificationIntent.putExtra("ms", message); 
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
        notification.setLatestEventInfo(context, title, message, intent); 
        notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    
        notification.defaults |= Notification.DEFAULT_SOUND; 
    
        notification.defaults |= Notification.DEFAULT_VIBRATE; 
        notificationManager.notify(0, notification);  
    } 
    
  4. 顯示來自推送新活動通知的數據。 答:這實現了當新的活動調用點擊通知時(從上面的第3點代碼)我從主要活動onCreate()中從SQLite獲取數據。

    DataBaseHelper dataBaseHelper = new DataBaseHelper(this); 
    dataBaseHelper.openDataBase(); 
    Cursor c = dataBaseHelper.getData(); 
    String data = null; 
    if(c.getCount()>0){ 
        if(c.moveToFirst()){ 
         do{ 
         data = c.getString(0); 
        } while(c.moveToNext()); 
        } 
    } else { 
        data = "No Data"; 
    } 
    
  5. 如果應用程序在通知點擊應用後關閉,這樣上手。 A.這個任務是從第3點實現的。

2

GCMIntentService.java

import com.google.android.gcm.GCMBaseIntentService; 
import com.google.android.gcm.GCMRegistrar; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.util.Log; 
/** 
* IntentService responsible for handling GCM messages. 
*/ 
public class GCMIntentService extends GCMBaseIntentService { 

    @SuppressWarnings("hiding") 
    private static final String TAG = "GCMIntentService"; 

    public GCMIntentService() { 
     super(SENDER_ID); 
    } 

    @Override 
    protected void onRegistered(Context context, String registrationId) { 
     Log.i(TAG, "Device registered: regId = " + registrationId); 
     displayMessage(context,"onregisterd"); 
     ServerUtilities.register(context, registrationId); 
    } 

    @Override 
    protected void onUnregistered(Context context, String registrationId) { 
     Log.i(TAG, "Device unregistered"); 
     displayMessage(context, "GCM unregistered"); 
     if (GCMRegistrar.isRegisteredOnServer(context)) { 
      ServerUtilities.unregister(context, registrationId); 
     } else { 
      // This callback results from the call to unregister made on 
      // ServerUtilities when the registration to the server failed. 
      Log.i(TAG, "Ignoring unregister callback"); 
     } 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.i(TAG, "Received message"); 
     String message =intent.getExtras().getString("message"); 
     displayMessage(context, message); 
     // notifies user 
     generateNotification(context,message); 
    } 

    @Override 
    protected void onDeletedMessages(Context context, int total) { 
     Log.i(TAG, "Received deleted messages notification"); 
     String message = ("total deleted"+ total); 
     displayMessage(context, message); 
     // notifies user 
     generateNotification(context, message); 
    } 

    @Override 
    public void onError(Context context, String errorId) { 
     Log.i(TAG, "Received error: " + errorId); 
     displayMessage(context, ("error:"+ errorId)); 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     // log message 
     Log.i(TAG, "Received recoverable error: " + errorId); 
     displayMessage(context, ("Recover error:"+ errorId)); 
     return super.onRecoverableError(context, errorId); 
    } 

    /** 
    * Issues a notification to inform the user that server has sent a message. 
    */ 
    private static void generateNotification(Context context, String message) { 
     int icon = R.drawable.icon; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, "Dear Customer , New Product has been Launched", when); 
     Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     notification.sound=soundUri; 
     String title = context.getString(R.string.app_name); 
     Intent notificationIntent = new Intent(context, lap_gcm.class); 
     notificationIntent.putExtra("message", message); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, notification); 
    } 

} 

結果活動

lap_gcm.java

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebView; 

public class lap_gcm extends Activity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     String message=getIntent().getStringExtra("message"); 
     //Here is Your message 

     } 
    } 

這對你提到我在一個使用博客的代碼庫我我開發應用程序。這將顯示新的通知接收通知,並在用戶點擊通知時開啓新的活動。

總是發送不要通過推送通知發送所有數據。你只需發送一些像數據這樣的小消息,然後從服務器中取出數據,一旦收到消息在設備中,然後將其存儲在數據庫中。

+0

您可以請給我的步驟如何在應用程序未運行時單擊通知時顯示新活動的推送通知數據。 –

+0

Intent notificationIntent = new Intent(context,lap_gcm.class); notificationIntent.putExtra(「message」,message); 在generateNotification 看到這個我通過intent發送消息到活動 – CoolMonster

+0

我跟着一樣,但是點擊通知數據沒有顯示。 –

1

通過推送通知

發送JSON數據你可以從你的服務器端代碼發送JSON數據的通知消息。一旦你收到通知,你就會在消息中收到一個JSON,你可以做任何你想做的事情。

將數據保存到SQLite數據庫

這是簡單的按照您的要求,您可以插入的JSON任何接收到的數據。解析後您可以從JSON獲取數據。

打開點擊推送通知的新活動。

你可以像下面

mNotificationManager = (NotificationManager) 
     this.getSystemService(Context.NOTIFICATION_SERVICE); 

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

NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(this) 
.setSmallIcon(R.drawable.ic_stat_gcm) 
.setContentTitle("GCM Notification") 
.setStyle(new NotificationCompat.BigTextStyle() 
.bigText(msg)) 
.setContentText(msg); 

mBuilder.setContentIntent(contentIntent); 
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

來自新活動的推送通知來顯示數據。

無論接收到推送消息,您都可以顯示數據,但必須解析JSON。

如果應用程序已關閉,所以點擊通知後,應用程序就開始了。

我的上面的代碼在這種情況下也適用於你。

看到這裏的JSON解析:http://www.vogella.com/tutorials/AndroidJSON/article.html

總而言之,你有當您從服務器推送到GCM,以後再執行解析添加在您的服務器鱈魚的JSON形式,你會得到的數據JSON並做任何你想做的事情。